How to Send SMS Using Java

Written by jackson lewis
  • Share
  • Tweet
  • Share
  • Email

Java developers have been able to send and receive e-mail 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 e-mail on their mobile phone. SMS text messages may be sent from Java applications using the Java Mail and Communications libraries.

Skill level:
Moderately Challenging

Other People Are Reading


  1. 1

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

  2. 2

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

  3. 3

    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; import; import java.util.Properties; import java.util.Date; import javax.mail.; import javax.mail.internet.; import javax.activation.;

  4. 4

    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";
        String password = "MyPassword";
        String smtphost = "";
        String compression = "My SMS Compression Information";
        String from = "";
        String to = "";
        String body = "Hello SMS World!";
        Transport my Transport = null;
  5. 5

    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();
         props.put("mail.smtp.auth", "true");
         Session mailSession = Session.getDefaultInstance(props, null);
         Message msg = new MimeMessage(mailSession);
         msg.setFrom(new InternetAddress(from));
         InternetAddress[] address = {new InternetAddress(to)};
         msg.setRecipients(Message.RecipientType.TO, address);
         msg.setSentDate(new Date());
  6. 6

    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.

         my Transport = mailSession.getTransport("smtp");
         myTransport.connect(smtphost, username, password);
         myTransport.sendMessage(msg, msg.getAllRecipients());
         } catch (Exception e) {
      public static void main(String[] argv) {
          SMTPSend smtpSend = new SMTPSend();


Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.