Link to home
Start Free TrialLog in
Avatar of preraksheth
preraksheth

asked on

Sending SMS from Java

How to send SMS using Java API? (If someone can help with free SMS in India, it would be great)
On a wider note, how to interface with Mobile network using desktop Java? Is it possible?
Could we call/divert mobile call using Java on desktop?
Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of preraksheth
preraksheth

ASKER

Thanks, but I should have added to my original question - "Thru internet".
In other words, I would like to send SMS thru internet to a desired number(or a set of numbers)
@samirbhogayta: please check my previous reply. The link that you have posted is the first link that i posted in my previous reply.
The solution is useful, but does not answer my original question completely about a free sms service in India. Anyhow, this is probabaly the closest that we can get
Here is some information on sending from Java. The messages are not free, but you do get a free trial account.

http://www.redoxygen.com/developers/java/

Java API

Use this API to send text messages from Java.

 Simple HTTP request
 Encrypted communication over SSL
 Long SMS (up to 765 characters)
 Delivery status via function return code
 Email replies

Example

public static int  SendSMS(String strAccountId,String strEmail,String strPassword,
String strMSISDN,String strMessage,StringBuffer strResponse)
{
      String  sRequestURL;
      String  sData;
      int nResult = -1;

      sRequestURL = "http://www.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 = Integer.parseInt(sResultCode);
            
            in.close();
      }
      
      catch (Exception e)
      {
            System.out.println("Exception caught sending SMS\n");
            nResult = -2;
      }
      return nResult;
}