Send SMS using JSMPP

In this article let’s discuss about sending SMS from a Java application using JSMPP API.

What is SMS?
Short Message Service is a service provided for exchanging short text messages between mobile phones, using SMPP protocol.

What is SMPP?
Short Message Peer-to-Peer (SMPP) is a standard protocol used in Telecommunication industry for exchanging SMS messages between SMS entities (Short Message Service Centers) over TCP/IP or X.25 connections.

SMPP is in turn based on request/response PDUs (Protocol Data Units) which are usually binary encoded.

Using the SMPP protocol, an SMS application system called the External Short Message Entity (ESME) may initiate an application layer connection with an SMSC over a TCP/IP or X.25 network connection and may then send short messages and receive short messages to and from the SMSC respectively.

There are few api's available for SMPP implementation in java, like SMPP API, Logica api etc, In this article we are going to deal with JSMPP, since it is very simple to use.

There are list of PDUs available as a part of SMPP specification like bind, unbind, generic_nack, submit_sm, submit_multi, deliver_sm etc.,

To send an SMS we need a list of entities to fill up the submit_sm PDU which is used for sending SMS messages.

Parameters

1. IP address of SMSC
2. Port to bind - SMSC
3. Bind parameter (transmitter, receiver or both) like BIND_TX, BIND_RX or BIND_TRX (required)
4. User Name to connect to SMSC
5. Password to connect to SMSC
6. Type of ESME system
7. Type of Number like NATIONAL, INTERNATIONAL, UNKNOWN, NETWORK SPECIFIC  
8. Numbering plan indicator
9. Service Type such as CMT, CPT, VMN, VMA, WAP, USSD

The above parameters are used to send the SMS (to SMSC) using the JSMPP api.

Have a look at the following class

SendSMS.java

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE IS DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package in.techdive.java;

import java.io.IOException;
import java.util.Date;

import org.jsmpp.InvalidResponseException;
import org.jsmpp.PDUException;
import org.jsmpp.bean.Alphabet;
import org.jsmpp.bean.BindType;
import org.jsmpp.bean.ESMClass;
import org.jsmpp.bean.GeneralDataCoding;
import org.jsmpp.bean.MessageClass;
import org.jsmpp.bean.NumberingPlanIndicator;
import org.jsmpp.bean.RegisteredDelivery;
import org.jsmpp.bean.SMSCDeliveryReceipt;
import org.jsmpp.bean.TypeOfNumber;
import org.jsmpp.extra.NegativeResponseException;
import org.jsmpp.extra.ResponseTimeoutException;
import org.jsmpp.session.BindParameter;
import org.jsmpp.session.SMPPSession;
import org.jsmpp.util.AbsoluteTimeFormatter;
import org.jsmpp.util.TimeFormatter;

public class SendSMS
{

    public SendSMS()
    {
        super();
    }

    public static void main(String[] args) throws Exception
    {
        SendSMS sendSms = new SendSMS();
        sendSms.sendTextMessage("9500000000");
    }

    private TimeFormatter tF = new AbsoluteTimeFormatter();

    /*
     * This method is used to send SMS to for the given MSISDN
     */

    public void sendTextMessage(String MSISDN)
    {

        // bind param instance is created with parameters for binding with SMSC
        BindParameter bP = new BindParameter(
                BindType.BIND_TX,
                "<user_name>",
                "<pass_word>",
                "<SYSTEM_TYPE>",
                TypeOfNumber.UNKNOWN,
                NumberingPlanIndicator.UNKNOWN,
                null);

        SMPPSession smppSession = null;

        try
        {
            // smpp session is created using the bindparam and the smsc ip address/port
            smppSession = new SMPPSession("<SMSC_IP_ADDRESS>", 7777, bP);
        }
        catch (IOException e1)
        {
            e1.printStackTrace();
        }
       
        // Sample TextMessage
        String message = "This is a Test Message";

        GeneralDataCoding dataCoding = new GeneralDataCoding(false, true,
                MessageClass.CLASS1, Alphabet.ALPHA_DEFAULT);

        ESMClass esmClass = new ESMClass();

        try
        {
            // submitShortMessage(..) method is parametrized with necessary
            // elements of SMPP submit_sm PDU to send a short message
            // the message length for short message is 140
            smppSession.submitShortMessage(
                    "CMT",
                    TypeOfNumber.NATIONAL,
                    NumberingPlanIndicator.ISDN,
                    "<MSISDN>",
                    TypeOfNumber.NATIONAL,
                    NumberingPlanIndicator.ISDN,
                    MSISDN,
                    esmClass,
                    (byte) 0,
                    (byte) 0,
                    tF.format(new Date()),
                    null,
                    new RegisteredDelivery(SMSCDeliveryReceipt.DEFAULT),
                    (byte) 0,
                    dataCoding,
                    (byte) 0,
                    message.getBytes());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

The above class consist of a method sendTextMessage(MSISDN), which sends a simple text message to the MSISDN using the submitShortMessage method of the JSMPP api.

First a connection is set up with the SMSC using the User name/Password/IP Address/Port etc,. Then SMPP Session object is created which consist of several overloaded methods to send a text message. The message length is 140.

In this way we can send a simple text message using JSMPP api in Java.

Related Articles:

Send Long SMS Message Using JSMPP

Send Ringtone Using JSMPP

Technology: 

Search