How to Send SMS Using Java

Save

Java developers have been able to send and receive email using the available Java libraries for a number of years. In order to keep up with technology that customers expect Enterprise solutions to provide, it has become necessary to use Java programming to send SMS text messages. Many customers expect to be able to receive SMS messages on their mobile phone for application updates and warnings since not all users have access to email on their cellular phone. SMS text messages may be sent from Java applications using the Java Mail and Communications libraries.

  • Download Mail.jar and Activation.jar (see Resources for links) and save to the Java library directory on your computer's local drive.

  • Start a new Java class in your Java Integrated Development Environment (IDE) and name it \"MyMobileJava.java\".

  • Enter the following Java libraries at the start of your Java class. These libraries include the required Java Mail and Communications API resources and other supporting Input/Output and Internet class libraries for sending SMS text messages.

    import java.io.
    GO
    import java.net.InetAddress
    GO
    import java.util.Properties
    GO
    import java.util.Date
    GO
    import javax.mail.

    GO
    import javax.mail.internet.
    GO
    import javax.activation.
    ;

  • Place the following Java code after the library import statements in order to instantiate the Java class and assign values for the default SMS text messages.

    public class SMTPSend {

    public SMTPSend() {
    }

    public void msgsend() {
    String username = \"MySMSUsername\"

    GO

        String password = \"MyPassword\"

    GO

        String smtphost = \"MySMSHost.com\"

    GO

        String compression = \"My SMS Compression Information\"

    GO

        String from = \"mySMSUsername@MySMSHost.com\"

    GO

        String to = \"PhoneNumberToText@sms.MySMSHost.com\"

    GO

        String body = \"Hello SMS World!\"

    GO

        Transport myTransport = null;
  • Create Java code to create a new communications session that will then be used to configure the information contained within a text message. This information will then be prepared to be sent. Enter the following Java code in your Java class at the end of the code entered in step four.

        try {
    Properties props = System.getProperties()

    GO

         props.put(\"mail.smtp.auth\", \"true\")

    GO

         Session mailSession = Session.getDefaultInstance(props, null)

    GO

         Message msg = new MimeMessage(mailSession)

    GO

         msg.setFrom(new InternetAddress(from))

    GO

         InternetAddress[] address = {new InternetAddress(to)}

    GO

         msg.setRecipients(Message.RecipientType.TO, address)

    GO

         msg.setSubject(compression)

    GO

         msg.setText(body)

    GO

         msg.setSentDate(new Date());
  • Send the text message by connecting to your SMS host address, saving changes to the message, and then sending the information. To do this, enter the following Java code to finish the Java class.

         myTransport = mailSession.getTransport(\"smtp\")

    GO

         myTransport.connect(smtphost, username, password)

    GO

         msg.saveChanges()

    GO

         myTransport.sendMessage(msg, msg.getAllRecipients())

    GO

         myTransport.close()

    GO

         } catch (Exception e) {
    e.printStackTrace()

    GO

         }
    }

    public static void main(String[] argv) {
    SMTPSend smtpSend = new SMTPSend()

    GO

          smtpSend.msgsend()

    GO

      }

    }

References

Promoted By Zergnet

Comments

Resources

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!