Java Project JSP JDBC Java Program Core Java Demo MORE

How to send SMS message in Java


No jar file needed for send SMS message in Java.

Here we using 1 files for send message in Java

  • SendSms.java:for Send message

SendSms.java

package com.om.otp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class SendSms {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    String authkey = "1245td5gh#hhdhdhdhhd";//Put your authentication key
    //Multiple mobiles numbers separated by comma
    String mobiles = "9437730730";
    //Sender ID,While using route4 sender id should be 6 characters long.
    String senderId = "ABCDEF";
    //Your message to send, Add URL encoding here.
    String message = "Test message";
    //define route
    String route="4";
    //Prepare Url
    URLConnection myURLConnection=null;
    URL myURL=null;
    BufferedReader reader=null;
    //encoding message
    String encoded_message=URLEncoder.encode(message);
    String mainUrl="http://malussms.com/api/sendhttp.php?";//put your sms gateway provider API
    //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();
    try
    {
        //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();
    }

   }

}