Sending SMS through JAVA code

 

In this tutorial we will see about sending SMS through Java code in India. For this first we need to chose some SMS gateway for sending SMS across India and other countries. I have chose www.SMSZone.in for this and will see small example for sending SMS.

Steps to get subscribed:
  • 1. For subscribing we need to provide registered Mobile no. and company / personal details. 
  • 2. Once subscribed need to decide whether we are going to use SMS for promotional or non-promotional activities. 
  • 3. Based on that we need decide our tariff plan and SMS template formats. 
  • 4. Next we need to chose the SenderId for SMS, like JDISCO (only 6 letters are allowed)
  • 5. Next you chose any language for sending SMS like Java, .NET, PHP etc.,


Lets see small Java code for sending SMS by using above SMS gateway.



public class SendSMS {
  
  // SMS Gateway API
  static String SMSApi = "http://www.smszone.in/sendsms.asp?page=SendSmsBulk&username=91XXXX12XXXX&password=XXXX&number=<PHONE>&message=<MSG>&SenderId=JDISCO";
  
  //List of mobile numbers to send SMS
  static String phoneNos = "91XXXX54XXXX, 91XXXX56XXXX";
  
  // SMS text
  static String smsText = "Hi All, Welcome to JavaDiscover";
    
  public static void main(String[] args) {
    try{
      String url = SMSApi.replace("<PHONE>", phoneNos).replace("<MSG>", smsText).replaceAll(" ""%20");
      String smsApiResponse = sendMySMS(url);
      System.out.println(smsApiResponse);
    }catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  public static String sendMySMS(String url){
    StringBuilder output = new StringBuilder();
    try{
      URL hp = new URL(url);
      System.out.println(url);
      URLConnection hpCon = hp.openConnection()
      BufferedReader in = new BufferedReader(new InputStreamReader(hpCon.getInputStream()));
      String inputLine;
      while ((inputLine = in.readLine()) != null)
        output.append(inputLine);
      in.close();
    }catch (Exception e) {
      e.printStackTrace();
    }
    return output.toString();
  }
}




Change you Username, password, phone number and Senderid accordingly. 





8 comments:
Write comments