How to Send an SMS Using JSP

Written by chanel adams
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Send an SMS Using JSP
You can send Java SMS to people's phones. (texting a message on pda device image by Paul Hill from

In the Java Server Pages (JSP), you can send and receive e-mails. However, with the right software and coding, you also can send and receive short message service (SMS) messages. SMS messages allow you to send brief messages from your computer to the recipient's mobile phone. Since not everyone has a mobile phone, Java programming makes it possible for you to send and receive messages. You may send and receive the messages in Java Applications using the Java Mail and Communications libraries.

Skill level:


  1. 1

    Download Mail.jar and Activation.jar. (See Resources.) Save the files to your Java directory. Start a new Java class in your Java Integrated Development Environment (IDE).

  2. 2

    Rename it "" Enter the following libraries in the beginning of your Java class:



    import java.util.Properties;

    import java.util.Date;

    import javax.mail.*;

    import javax.mail.internet.*;

    import javax.activation.*;

    These libraries allow you to send and receive SMS messages in Java.

  3. 3

    Insert the following code after the statements:

    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;

    This lets you initiate Java class and assign values for the SMS messages.

  4. 4

    Create a code that you will use to configure the information within a SMS message. You will use the information to send the message. Enter the code at the end of the previous codes.

    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());

  5. 5

    Connect the SMS message to your SMS host address. Save changes to the message and send the information. To do this, enter the following code:

    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.