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

To send MMS messages from Java, please see:

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. (Binary SMS support is not supported by many SMSC connections.)

The class supports many of the URL parameters that are defined for NowSMS, and could easily be adapted to support additional parameters.

The following example illustrates how the class is used.

Example – Sending a simple SMS text message:


sendsms.server = "";

sendsms.user = "username";

sendsms.password = "password";

sendsms.phonenumber = "+9999999999";

sendsms.text = "This is a test message";


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:


The init method initialises the SMS message object.

sendsms.server = "";

The server method sets the URL address for the NowSMS server.


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.) = "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. = "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.server = "";

sendsms.user = "username";

sendsms.password = "password";

sendsms.phonenumber = "+9999999999";

sendsms.text = "This is a test message";


Example – Sending a text message to a specific application port for a Java applet running on the phone:


sendsms.server = "";

sendsms.user = "username";

sendsms.password = "password";

sendsms.phonenumber = "+9999999999";

sendsms.text = "This is a test message";

sendsms.destport = "3333";


Example – Sending a WAP Push Message:


sendsms.server = "";

sendsms.user = "username";

sendsms.password = "password";

sendsms.phonenumber = "+9999999999";

sendsms.text = "This is a test message";

sendsms.wapurl = "";


The complete Java class is displayed below. This script can also be downloaded from




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(); 




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




             connection.disconnect() ; 



       catch(IOException e) {

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


       return returnstring;




System Requirements for using this script:

This script connects to a NowSMS server and posts a request to the NowSMS server to send a message.

If you do not have a NowSMS server installed, this script will not work.

NowSMS is available as a cloud service, or can be installed on a Windows PC. To send SMS or MMS messages, NowSMS also requires a GSM modem, Android phone, or SMS service provider connection.

Free 30-day trial versions of the NowSMS software can be downloaded at

Either the Now SMS/MMS Gateway or NowSMS Lite can be used. NowSMS Lite can send SMS and MMS messages using a single GSM modem or Android phone as a modem connection. The full product supports multiple modems and/or service provider connections.

Before attempting to interface with NowSMS using scripts, you should verify that NowSMS is configured correctly and can send SMS messages using its built in web interface. For Android phone or GSM modem connections, there are quick start guides that can be found here:

Once you have verified that NowSMS is working on its own, it will be necessary to change IP addresses referenced in the script to point to your NowSMS server. Our sample scripts use an IP address of, which is a special loopback address for the current PC. If your script is running on a different system than the NowSMS server, change to a host name or IP address that is valid for your environment. It is also necessary to define an SMS user account on the NowSMS server with account credentials that match with the user account in the script.