2016-08-29

Send SMS Text Message with Java

The following example Java class, sendsms, can be used to send an SMS text message via NowSMS from Java. This script can also be downloaded from http://www.nowsms.com/download/sendsms.java.txt.


import java.net.*; 

import java.io.*; 



public class sendsms {

 



    public static String server;

    public static String user;

    public static String password;

    public static String phonenumber;

    public static String text;

    public static String data;

    public static String udh;

    public static String pid;

    public static String dcs;

    public static String sender;

    public static String validity;

    public static String servicetype;

    public static String smscroute;

    public static String receiptrequested;

    public static String sourceport;

    public static String destport;

    public static String delayuntil;

    public static String voicemail;

    public static String wapurl;

    public static String wapsl;



    public static String url_str;



    public static void init () {

        server = null;

        user = null;

        password = null;

        phonenumber = null;

        text = null;

        data = null;

        udh = null;

        pid = null;

        dcs = null;

        sender = null;

        validity = null;

        servicetype = null;

        smscroute = null;

        receiptrequested = null;

        sourceport = null;

        destport = null;

        delayuntil = null;

        voicemail = null;

        wapurl = null;

        wapsl = null;

    }



    public static void setvar (String argname, String argvalue) {



       if (argname != null) {

          if (argvalue != null) {

             url_str = url_str + "&" + argname + "=";

             try {

                String encoded = URLEncoder.encode (argvalue, "UTF-8");

                url_str = url_str + encoded;

             }

             catch (UnsupportedEncodingException e) {

                url_str = url_str + argvalue;

             }

          }

       }

           

    }



    public static String send () {





       String returnstring;

   

       returnstring = null;



       if (server == null) {

  	  System.out.println("sendsms.server value not set");

          return returnstring;

       }



       url_str = server + "?";

       setvar("user", user);

       setvar("password", password);

       setvar("phonenumber", phonenumber);

       setvar("text", text);

       setvar("data", data);

       setvar("udh", udh);

       setvar("pid", pid);

       setvar("dcs", dcs);

       setvar("sender", sender);

       setvar("validity", validity);

       setvar("servicetype", servicetype);

       setvar("smscroute", smscroute);

       setvar("receiptrequested", receiptrequested);

       setvar("sourceport", sourceport);

       setvar("destport", destport);

       setvar("delayuntil", delayuntil);

       setvar("voicemail", voicemail);

       setvar("wapurl", wapurl);

       setvar("wapsl", wapsl);



       try {

          URL url2=new URL(url_str); 



          HttpURLConnection connection = (HttpURLConnection) url2.openConnection(); 

          connection.setDoOutput(false); 

          connection.setDoInput(true); 



          String res=connection.getResponseMessage(); 

		

          System.out.println("Response Code  ->"+res); 

	

          int code = connection.getResponseCode () ; 

		

          if ( code == HttpURLConnection.HTTP_OK ) {

             //Get response data.

             BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));



             String str;

        

             while( null != ((str = in.readLine()))) {

                if (str.startsWith("MessageID=")) {  

                   returnstring = returnstring + str + "\r\n";

                   System.out.println(str);

                }

             }    

             connection.disconnect() ; 

          }

       }

       catch(IOException e) {

          System.out.println("unable to create new url"+e.getMessage());

       }

       return returnstring;

   }

}

The sendsms class defined in this example can be used to send simple SMS text messages, as well as many types of binary SMS messages, including WAP Push.
The class supports many of the URL parameters that are defined for NowSMS, and could easily be adapted to support additional parameters.
NowSMS URL parameters are supported as methods for the sendsms class, with method names matching the URL parameter names, except that all methods are in lower case.
In addition the URL parameter methods, the following additional methods are defined:
sendsms.init();
The init method initialise the SMS message object.
sendsms.server = "http://localhost:8800/";
The server method sets the URL address for the NowSMS server.
sendsms.send();
The send method submits the SMS message to NowSMS. (The send method returns a list of message ids assigned for the submitted message, with one message id per line, in the following format: MessageID=xxxxxxxxxxxxxx.req, Recipient=xxxxxxxxxxxx)
Supported methods for setting URL parameters:
sendsms.user = "username";
sendsms.password = "password";
These methods specify the authorisation credentials of the “SMS Users” account that is submitting the message.
sendsms.phonenumber = "recipient";
This method specifies the recipient phone number to which this message should be addressed. This can be a comma delimited list of recipient phone numbers or a distribution list name.
sendsms.text = "text of message";
This method specifies the text of the message. (Or for WAP Push messages, the text associated with the WAP Push URL.)
sendsms.data = "hexstring";
This method specifies a hex string of binary data for sending a binary SMS message.
sendsms.udh = "hexstring";
This method specifies a hex string of binary data for the User Data Header (UDH) of the SMS message.
sendsms.pid = "PID Value";
This method specifies a hex value representing the SMS Protocol ID (PID) of this SMS message.
sendsms.dcs = "DCS Value";
This method specifies a hex value representing the value of the SMS Data Coding Scheme (DCS) for this message.
sendsms.sender = "Sender";
This method specifies the sender (source) address to be specified for this message. (Note: It is not possible to override the sender address when sending messages via a GSM modem.)
sendsms.smscroute = "route name";
This method specifies an outbound SMSC route to be used for the message. (For more information on SMS message routing, see SMS Message Routing Logic.)
sendsms.receiptrequested = "Yes";
This method can be used to request a delivery receipt.
sendsms.sourceport = "3333";
sendsms.destport = "3333";
These methods can be used to specify source and destination ports for routing the SMS message to a specific application on the recipient mobile phone.
sendsms.delayuntil = "YYYYMMDDHHMM";
This method allows messages to be submitted to NowSMS and queued for later processing. The value of this parameter should be of the format “YYYYMMDDHHMM”, indicating the date and time until which the message should be delayed, where YYYY is the year, MM is the month, DD is the day, HH is the hour (in 24 hour format), and MM is the minutes.
sendsms.wapurl = "http://x/path";
Specifies that the a WAP Push message should be sent, and this is the URL to be sent in the WAP Push message.
Example – Sending a simple text message:
sendsms.init();
sendsms.server = "http://127.0.0.1:8800/";
sendsms.user = "test";
sendsms.password = "test";
sendsms.phonenumber = "+9999999999";
sendsms.text = "This is a test message";
sendsms.send();
Example – Sending a text message to a specific application port for a Java applet running on the phone:
sendsms.init();
sendsms.server = "http://127.0.0.1:8800/";
sendsms.user = "test";
sendsms.password = "test";
sendsms.phonenumber = "+9999999999";
sendsms.text = "This is a test message";
sendsms.destport = "3333";
sendsms.send();
Example – Sending a WAP Push Message:
sendsms.init();
sendsms.server = "http://127.0.0.1:8800/";
sendsms.user = "test";
sendsms.password = "test";
sendsms.phonenumber = "+9999999999";
sendsms.text = "This is a test message";
sendsms.wapurl = "http://www.nowsms.com/";
sendsms.send();

No comments:

Post a Comment