Wednesday, January 4, 2012

Sending SMS to your Mobile Through Java Program

Yes,we can send SMs Using our Java Program.These are the steps
Note:Ipipi allows you to send only 5 sms free of cost,After that create one more account
1> create an account in http://www.ipipi.com/
2>I created Account like this:
username:  surajSMS
password: suraj123

3>Prepare the program in this way





-------------------------------------------------------------------------------------------------
SendSMS.java
 ------------------------------------------------------------------------------------------------
import java.io.*;                                                                                   
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendSMS {

    public SendSMS() {
    }
   //create an account on ipipi.com with the given username and password
    public void msgsend() {
        String username = "surajSMS";  //Your Credentials
        String password = "suraj123";
        String smtphost = "ipipi.com";      //Ip/Name of Server
        String compression = "None";       //I dont want any compression
        String from = "surajSMS@ipipi.com";    //ur userid@ipipi.com
        //This mobile number need not be registered with ipipi.com
        String to = "919861098610@sms.ipipi.com";    //mobile number where u want to send sms
        String body = "Hi This Msg is sent through Java Code";
        Transport tr = null;

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

         tr = mailSession.getTransport("smtp");
         //try to connect
         tr.connect(smtphost, username, password);
         msg.saveChanges();
         //send msg to all recipients
         tr.sendMessage(msg, msg.getAllRecipients());
         tr.close();
         } catch (Exception e) {
             e.printStackTrace();
         }
    }

      public static void main(String[] argv) {
         SendSMS sms = new SendSMS();
          sms.msgsend();
          System.out.println("Successfull");
      }
}


Download Source Here

9 comments:

  1. It is giving error
    javax.mail.MessagingException: Unknown SMTP host: ipipi.com;

    Please help

    ReplyDelete
    Replies
    1. Hi trib,I think some mistake was done while adding jars,please download the source code given my me,i have also included jars there,open a new account,add those jars,modify the necessary details,and it will sure work fine.

      Delete
    2. I have added your Jar files to classpath and executed the above class
      still i got the runtime exception like
      ---------- java ----------
      javax.mail.MessagingException: Could not connect to SMTP host: ipipi.com, port: 25;
      nested exception is:
      java.net.ConnectException: Connection timed out: connect
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
      at javax.mail.Service.connect(Service.java:275)
      at javax.mail.Service.connect(Service.java:156)
      at SendSMS.msgsend(SendSMS.java:48)
      at SendSMS.main(SendSMS.java:60)
      Caused by: java.net.ConnectException: Connection timed out: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
      at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
      at java.net.Socket.connect(Socket.java:519)
      at java.net.Socket.connect(Socket.java:469)
      at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
      at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
      ... 5 more
      Successfull

      Output completed (21 sec consumed) - Normal Termination


      Not received any SMS

      Delete
  2. it is getting sucess..bt not getting sms.

    ReplyDelete
  3. all r fne ..it is giving sucess..i am not gettting sm..

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. i am also not getting sms but build sucess with authentication error

    ReplyDelete
  6. how can i create account on ipipi.com? i fill info but no place for username, password

    ReplyDelete
  7. how can i create account on ipipi.com? i fill info but no place for username, password

    ReplyDelete