Saturday, May 9, 2015

How to send SMS in java.

To send a sms in java is very similar to send email in java. At the place of mail server (smtp host) we need to give some third party (which are providing the sms service) smtp host.
Many number of SMS provider in the market we can choose any of them. Here in my below 2 example I have choose two sms provider

1-    ipipi.com
2-    vianett.com

First example is using ipipi.com and second one is vianett.com.

To sending SMS by using above vendor we need to register with them and they will provide below details-
Username
Password
Smtp host/port
Some of the vendor provide API as well we need to add related jar in our application and send the sms using their provided method. In case of Vianett.com they are providing the API and for ipipi.com we can use our mail program (http://atozjavatutorials.blogspot.in/2015/05/how-to-send-mail-in-java.html) only we need to change host and port.

Example 1:
Using ipipi.com

Step 1- Register on ipipi.com then you will get username, password, smtphost, from (username followed by @ipipicom), to (mobile number followed by @sms.ipipi.com)

1
2
3
4
5
6
String username= "ashishsingh";
 String password="ashishsingh";
 String smtphost="ipipi.com";
 String compression="None";
 String from="ashishsingh@ipipi.com";
 String to="8143514825@sms.ipipi.com";

Step 2- Create property like below-


1
2
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");

Step 3- Create a session,message and trasport

1
2
3
4
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
tr.connect(smtphost,username,password);
msg.saveChanges();

Step 4- Send the sms.

1
tr.sendMessage(msg, msg.getAllRecipients());

SMS.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.atozjavatutorials;

import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SMS {
    String username= "ashishsingh";
    String password="ashishsingh";
    String smtphost="ipipi.com";
    String compression="None";
    String from="ashishsingh@ipipi.com";
    String to="8143514825@sms.ipipi.com";
    String body="This is test massage";
    Transport tr= null;
    SMS() {
        Properties props = System.getProperties();
        props.put("mail.smtp.auth", "true");
        try {
            Session session = Session.getDefaultInstance(props, null);
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject("this is test mail subject");
            msg.setText(body);
            msg.setSentDate(new Date());
            tr = session.getTransport("smtp");
            tr.connect(smtphost,username,password);
            msg.saveChanges();
            tr.sendMessage(msg, msg.getAllRecipients());
            tr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        SMS mail = new SMS();
        System.out.println("mail send!");
    }
}

Example 2-  Using vianett.com

Step 1- Register on vianett.com and download the "vianett_sms_version.jar". I have download the "vianett_sms_v1_6.jar". Once you will register you will get username, password, host and port.

Step 2- Initialize the SmsTransceiver by calling transceiver.initialize( smsHost, Integer.parseInt( smsPort ), smsUsername, smsPassword, new SmsScreenLogger() ); method.

Step 3- Create SMS using sms object

1
2
3
4
5
6
Sms sms = new Sms();
sms.setId( ++this.counter );
sms.setReplyPath( 100 );
sms.setSender( "8712137804" ); // Set the sender number.
sms.setMessage( "This SMS test!" );
sms.setRecipient( "8143514825" ); // The recipients phone number.

Stpe 4- Send the sms

1
transceiver.send( sms );

SmsSender.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.atozjavatutorials;
import no.vianett.sms.SmsEventListener;
import no.vianett.sms.Sms;
import no.vianett.sms.SmsEvent;
import no.vianett.sms.component.SmsTransceiver;
import no.vianett.sms.log.SmsScreenLogger;
import no.vianett.sms.event.SmsDeliveredEvent;
import no.vianett.sms.event.SmsSendingFailedEvent;
import no.vianett.sms.event.SmsDeliveryFailedEvent;
 
public class SmsSender implements SmsEventListener
{
    private SmsTransceiver transceiver = null;
    private Object link = null; // Just to keep this object alive.
    private int counter = 0;
 
    public SmsSender()
    {
        this.link = this; 
        this.transceiver = SmsTransceiver.getInstance(); // Get the transceiver object.
        // Initialize transceiver.
        String smsHost = "cpa.vianett.no";
        String smsPort = "31339";
        String smsUsername = "ashishkumarsingh112233@gmail.com";
        String smsPassword = "ashishkumarsingh";
        this.transceiver.initialize( smsHost, Integer.parseInt( smsPort ), smsUsername, smsPassword, new SmsScreenLogger() );
        this.transceiver.addSmsEventListener( this ); // Registrer this class as listener for SMS events.
        // Send message
        Sms sms = new Sms();
        sms.setId( ++this.counter );
        sms.setReplyPath( 100 );
        sms.setSender( "8712137804" ); // Set the sender number.
        sms.setMessage( "This SMS test!" );
        sms.setRecipient( "8143514825" ); // The recipients phone number.
        this.transceiver.send( sms );
    }
 
    public static void main( String[] args ){ new SmsSender(); }
    public void eventHappened( SmsEvent event )
    {
        if( event instanceof SmsDeliveredEvent )
        {
            System.out.println( "Sms delivered." );
            System.out.println( "Refno : " + event.getReferenceId() );
            System.out.println( "Sms generated by : " + event.getSource().getClass() );
        }
        else if( event instanceof SmsSendingFailedEvent )
        {
            System.out.println( "Sms sending failed." );
            System.out.println( "Refno : " + event.getReferenceId() );
            System.out.println( "Return code : " + ( ( SmsSendingFailedEvent ) event ).getReturnCode() );
            System.out.println( "Sms generated by : " + event.getSource().getClass() );
        }
        else if( event instanceof SmsDeliveryFailedEvent )
        {
            System.out.println( "Sms delivery failed." );
            System.out.println( "Refno : " + event.getReferenceId() );
            System.out.println( "Error code : " + ( ( SmsDeliveryFailedEvent ) event ).getErrorCode() );
            System.out.println( "Sms generated by : " + event.getSource().getClass() );
        }
    }
}

No comments :

Post a Comment