SMS Send Service In Java

Posted By : Vakeel Malik | 22-Apr-2018

Introduction:-

We are using SMS91 to send the message. this provides the API  which will help us in sending the message. 

First of all, you need to register in SMS 91 this URL https://sms91.com.  After registration this you can generate  Authentication ID. This Authentication Id will be unique.

 

Like authkey=dsds83y8bkbdkjsbdsncsbs.

 

This service provides free SMS sending in all India 24 H. 

You need to generate following keys.

a. Authentication Key
b. Sender ID
c. Route

 

There is some description of all keys.

 

1.AUTHKEY:-
           This is a User authentication key. it should be unique.

2.TEXT:- 

      This key  contains the URL encoded message content to send

3.SENDER:- 

     This key contains String sender ID. Ex Sender "name".

4. TO:-

    This key contains the mobile number of receiver user. It contains a scheduled date and time FLASH

5. ROUT: 

   This key contains a numeric string  value

   Example String  Route ="5";

6. COUNTRY

 It contains country code for India 91 

   

What is Sender ID:- 

Sender ID is A identity number of the sender. when you send SMS to your friend then Sender id will display on receiver end at top of the panel. you can choose according to you it should be a string value.

What is authentication key:-

Authentication key generates after registration of in sms91 service for every user. on the top of page, you can find your own auth key.  this key will use in SMS sending code.

 

There are following reason for the failed message:-

One and important reason is time out.

Second reason full the memory.

Network fail and week network connection

Number not available

Not identified  service centre address

Fail message delivery. 

 

Now We are going to code part.  it is simple to understand bellow code. 

code for send sms:-

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

@SuppressWarnings("unchecked")
public class SendSms{

        public static void main(String[] args)
    	{
           //254720871246
            //Your authentication key
            String authkey = "193194A67Jnyn45a5c67t3";
            //Multiple mobiles numbers separated by comma
            String mobiles = "+254720871246";
            //Sender ID,While using route4 sender id should be 6 characters long.
            String senderId = "vakeelMalik";
            //Your message to send, Add URL encoding here.
            String message = "hello  how r u";
            //define route
            String route="4";

            //Prepare Url
            URLConnection myURLConnection=null;
            URL myURL=null;
            BufferedReader reader=null;
          try
            { 

            //encoding message
            String encoded_message=URLEncoder.encode(message,java.nio.charset.StandardCharsets.UTF_8.toString());

            //Send SMS API
            String mainUrl="http://api.msg91.com/api/sendhttp.php?";

            //Prepare parameter string
            StringBuilder sbPostData= new StringBuilder(mainUrl);
            sbPostData.append("authkey="+authkey);
            sbPostData.append("&mobiles="+mobiles);
            sbPostData.append("&message="+encoded_message);
            sbPostData.append("&route="+route);
            sbPostData.append("&sender="+senderId);

            //final string
            mainUrl = sbPostData.toString();
            
                //prepare connection
                myURL = new URL(mainUrl);
                myURLConnection = myURL.openConnection();
                myURLConnection.connect();
                reader= new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
                //reading response
                String response;
                while ((response = reader.readLine()) != null)
                //print response
                System.out.println(response);

                //finally close connection
                reader.close();
            }
            catch (IOException e)
            {
                    e.printStackTrace();
            }
        }
}

About Author

Author Image
Vakeel Malik

Vakeel is a bright Java Developer working in Oodles.

Request for Proposal

Name is required

Comment is required

Sending message..