Sending SMS using SendSMS API in JAVA

Sendsms.pk is the leading company in Pakistan in providing free SMS to all the networks like, Zong, Moblink, Telenor, Warid and Uphone etc, it doesn''t matter if you are anywhere in Pakistan while, you can receive messages from all over the world.

Here is the complete source code how you will use this API and can send message from your PC to mobile.

In order to send SMS using  Sendsms.pk, you will need to register your cell number here



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

 public class SendSMS {
        static String mobile_number = "YOUR_MOBILE_NUMBER";
        static String msgBody = "This is test message body...";
        static String fileText;
       
   public static void main(String[] args) throws IOException {
      SendSMS sms = new SendSMS();      
      String stringReturned = sms.sendSMSUsingGateWay(mobile_number,msgBody);
      System.out.println(stringReturned);
     }
             
     String sendSMSUsingGateWay(String mobileNumbers, String fvarSMSText)
     {
            //Variable to store sent count/return string;
        int sentCount = 0;
        String returnString = null;
       
        //Variable To Store Mobile Numbers if Mobile Numbers Exist
        if (mobileNumbers != null)
        {
              
        String[] mobileNumberSplit = mobileNumbers.split(";");
        //Loop Through Mobile Numbers
         for (int numberCounter = 0; numberCounter < mobileNumberSplit.length; numberCounter++)
         {
           try
           {
            //Build parameter string
            String parameters = "phone=" + mobileNumberSplit[numberCounter]+ "&msg=" + fvarSMSText+ "&type=0";
                 //http://api.sendsms.pk/sendsms/ae6cf5a01a85c31dcb3d.json?phone=00923334373753&msg=testmessage&type=0
            URL url = new URL("http://api.sendsms.pk/sendsms /f77a27fc63158454c821.json?");
            HttpURLConnection httpCon = (HttpURLConnection)url.openConnection();
            httpCon.setReadTimeout(90000);
            httpCon.setDoOutput(true);
            httpCon.setRequestMethod("POST");
                         
            //Send request
            OutputStreamWriter out = new OutputStreamWriter( httpCon.getOutputStream());
            out.write(parameters);
            out.flush();
                         
            // Get the response
            BufferedReader in = new BufferedReader(new InputStreamReader (httpCon.getInputStream(), "UTF-8"));    
            String line;
            StringBuffer response = new StringBuffer();
            while((line = in.readLine()) != null) {       
                response.append(line);                                           
            }
            returnString = response.toString();
            System.out.println(returnString);
                         
            in.close();
            out.close();
            if (returnString.toUpperCase().contains("successfully"));
            {
              fileText = mobileNumberSplit[numberCounter]+ "\t" + fvarSMSText;
              //Increment Sent SMS Count
               sentCount++;
            }
          }//Catch Exception and Do Nothing
          catch (Exception ex)
          {
            return ex.getMessage();
          }
           
      }
      return "SMS Sent To : " + sentCount + " / " + (mobileNumberSplit.length-1) + " Resources";
  }
  else
  {                     
    return "SMS Not Sent";
  }
 }
}  

1 comment:

  1. Msgclub provides you easiest way to use with sample codes in various languages including Java. If you are a businessman and looking for an effective marketing tool, SMS API java is the answer for you.

    ReplyDelete