Monday, 3 December 2012

In this blog I will explain how to send a SMS from java application by making use of a Ipipi SMS service.

Prerequirements:
  1. Mail.jar from JavaMail (You can download it from http://java.sun.com/products/javamail/downloads/index.html
  2. Activation.jar (You can download it from http://java.sun.com/products/javabeans/jaf/downloads/index.html
  3. Smtp.jar(You can download it from http://www.java2s.com/Code/Jar/s/Downloadsmtpjar.htm)
  4. An account needs to be created at www.ipipi.com 
After creating your account and finishing the downloading part add the jars to the classpath.

Sample code for sending text messages:
 
  public class SendSMS{

  public static void main(String args[]){
   SendSMS send = new SendSMS();
    send.sendMessage();
   }
  public void sendMessage(){
        String username = "Your IPIPI account username";//"tryme"
        String password = "Your IPIPI account password";//"password"
        String smtphost = "ipipi.com";
        String from = "YourIPIPIUsername@ipipi.com";//"tryme@ipipi.com"
        String to = "Receiver'sPhoneNumber@sms.ipipi.com";       
        //"91XXXXXXXXXX@sms.ipipi.com"
        String compression = "Compression option goes here";
        String body = "Your message";
        Transport transport = null;

        Properties props = System.getProperties();
        props.put("mail.smtp.auth", "true");
     
        //Get a session object.
        Session mailSession = Session.getDefaultInstance(props, null);

        //Construct the message.
        Message msg = new MimeMessage(mailSession);

       //Set message attributes.
        msg.setFrom(new InternetAddress("from"));
        InternetAddress[] address = { new InternetAddress(to) };
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject(compression);
        msg.setText(body);
        msg.setSentDate(new Date());

        transport = mailSession.getTransport("smtp");
        transport.connect(smtphost, username, password);
        msg.saveChanges();
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();
        System.out.println("Message sent");
}
}

Username: Your ipipi.com username (the same account you used to login to www.ipipi.com - Forexample: tryme)
Password: Your ipipi.com password (the same account you used to login to www.ipipi.com - Forexample: password)
Sent from: Your account username + @ipipi.com (For example: tryme@ipipi.com). It is very important to add @ipipi.com to this field, otherwise error message will be generated ("503 Incorrect authentication for specified email address").

Using several compression algorithms ipipi SMS service shrinks incoming text so that more information can be transferred. For example it deletes spaces, removes short words and concatenate long words.

If not required the compression parameter can be left blank.