Showing posts with label Send sms in Java. Show all posts
Showing posts with label Send sms in Java. Show all posts

Tuesday 10 July 2012

Sending Sms in JAVA


Hello friends have you searching of sending mobile alert or mobile
Notification from your Java web Application ?

If, Yes then this blog Helps you a lot.

While lot of searches I came across to Red Oxygen SMS gateway.
It provide a few free messages on the daily basis for your testing
Purpose.

For sending sms from your application first of all you have to sign
Up  your account in Red Oxygen .

Once you create your account in Red Oxygen you receive a mail
Which contains:

Account Name:
Mukesh Yadav
Account ID:
CI0007555890
Email Address:
Password:
************


Now  Place the following code in your web application and Enjoy the
Mobile alert facility.




/*
 *
 *  Java example code to send an SMS via the Red Oxygen SMS gateway over HTTP.
 *
 *  Mukesh Yadav   
 */

import java.io.*; 
import java.net.*; 
import java.lang.*; 



public class RedOxygenSMS
{ 

 public static int  SendSMS(String strAccountId,String strEmail,
        String strPassword,String strMSISDN,String strMessage,
        StringBuffer strResponse)
 {
  String  sRequestURL;
  String  sData;
  Integer nResult = -1;
 
  sRequestURL = "http://sms1.redoxygen.net/sms.dll?Action=SendSMS";

  try
  {  
  
   sData  = ("AccountId="  +       URLEncoder.encode(strAccountId,"UTF-8"));
   sData += ("&Email="     + URLEncoder.encode(strEmail,
                        "UTF-8"));
   sData += ("&Password="  + URLEncoder.encode(strPassword,
                        "UTF-8"));
   sData += ("&Recipient=" + URLEncoder.encode(strMSISDN,
                        "UTF-8"));
   sData += ("&Message="   + URLEncoder.encode(strMessage,
                        "UTF-8"));


  
   URL urlObject = new URL(sRequestURL); 
   
   HttpURLConnection con = (HttpURLConnection)  urlObject.openConnection();
   con.setRequestMethod("POST");
   con.setDoInput (true);
                        con.setDoOutput (true);


   DataOutputStream out;
              out = new DataOutputStream(con.getOutputStream());
              out.writeBytes (sData);
   out.flush();
   out.close();
      
   BufferedReader in = new BufferedReader
                          (new InputStreamReader(con.getInputStream())); 
     
   String inputLine; 
   StringBuffer responseBuffer = new StringBuffer(); 

   while ((inputLine = in.readLine()) != null)
   {
                              responseBuffer = responseBuffer.append(inputLine);
         responseBuffer = responseBuffer.append("\n\n\n");
   }
  
   strResponse.replace(0,0,responseBuffer.toString());

   String sResultCode = strResponse.substring(0,4);
   nResult = new Integer(sResultCode);
   
   in.close();
  }
  
  catch (Exception e)
  {
   System.out.println("Exception caught sending SMS\n"); 
   nResult = -2;
  }
  return nResult;
 }


 public static void main(String[] args) throws Exception 
 { 
  String strAccountId  = "CI00001234";  // Put your AccountId here
  String strEmail      = "youremal@company.com";  // Put your Email address here (Used for authentication and replies)
  String strPassword   = "yourpassword";  // Put your Password here
  String strMSISDN     = "61407000000";   // Put a recipient mobile number here
  String strMessage    = "Test SMS via Red Oxygen API";  // Put your SMS message text here
  Integer nResult;
  StringBuffer strResponse = new StringBuffer();

  nResult = SendSMS(strAccountId,strEmail,strPassword,strMSISDN,strMessage,strResponse);

                System.out.println("Result Code = " + nResult + "\n");
  System.out.println("Response Text = " + strResponse + "\n");

 } 
} 




Hope this will help you…
Enjoy Coding J

 

Copyright @ 2013 Android Developers Blog.