Send SMS from Java (or JSP)

Posted by on Feb 4, 2009 in Support Blog

Topic Keywords: ,

A Java example for sending SMS messages via NowSMS has been posted at the following link:

http://www.nowsms.com/download/sendsms.java.txt

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 most of the URL paremeters that are defined for NowSMS at the following link:

http://www.nowsms.com/doc/submitting-sms-messages/url-parameters

Additionally, the class supports many additional parameters that have been added in newer releases of NowSMS.

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)

Additional supported methods:

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 http://www.nowsms.com/routing-sms-messages-to-a-specifc-smsc-route.)

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. (For more information, see http://www.nowsms.com/sms-port-number.)

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.

Examples

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

For comments and further discussion, please click here to visit the NowSMS Technical Forums (Discussion Board)...

2 Responses to “Send SMS from Java (or JSP)”

  1. prafull says:

    hey it is giving a msg……

    unable to crate new url connection

  2. Brett Warthen says:

    You need to modify the following line:

    sendsms.server = "http://127.0.0.1:8800/";

    .. so that it points to your NowSMS server.

    For technical support, please go to our discussion board at http://www.nowsms.com/messages