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

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:
Email Address:

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";

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

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

   DataOutputStream out;
              out = new DataOutputStream(con.getOutputStream());
              out.writeBytes (sData);
   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");

   String sResultCode = strResponse.substring(0,4);
   nResult = new Integer(sResultCode);
  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

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....


  1. hi,

    i´m trying to send sms from an android aplication using red oxigen.

    i used your code, but does not work. i get result code -2

    how can i fixe this??




Copyright @ 2013 Android Developers Blog.