Wednesday, August 14, 2013

Java Code to send SMS through SMPP

Java Code to send sms using smpp:

import java.io.IOException;
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.extra.SessionState;
import org.jsmpp.session.BindParameter;
import org.jsmpp.session.SMPPSession;

/**
 *
 * @author paul.pronabananda
 */
public class SMS {

    private String serverAddress = "xxx.xx.xxx.xx"; (ip address of smpp server)
    private int port = xxxx; (port no to connect)
    private String systemID = "paul";  (user id to establish connection)
    private String systemType = "paul";
    private String password = "paul";
    private TypeOfNumber sourceTON = TypeOfNumber.valueOf((byte) 5);
    private TypeOfNumber destinationTON = TypeOfNumber.valueOf((byte) 1);
    private NumberingPlanIndicator sourceNPI;
    private NumberingPlanIndicator destinationNPI;
//    private String fromNumber = "01732444471";
    private String prefixToNumber = "88";
//    private String toNumber = "01553466667";// private TimeFormatter tF = new AbsoluteTimeFormatter();

    public void sendTextMessage(String MSISDN,String fromNumber,String msgBody) {
        sourceNPI = NumberingPlanIndicator.valueOf((byte) 9);
        destinationNPI = NumberingPlanIndicator.valueOf((byte) 1);

        BindParameter bindParam;
        bindParam = new BindParameter(BindType.BIND_TX, systemID, password, systemType, sourceTON, sourceNPI, null);
        SMPPSession smppSession = null;
        try {
            // smpp session is created using the bindparam and the smsc ip address/port
            smppSession = new SMPPSession();
            smppSession.unbindAndClose();
            smppSession.connectAndBind(serverAddress, port, bindParam);
        } catch (IOException e1) {
            System.err.println("Problem to create Session");
            e1.printStackTrace();
        }

      //  String msgBody = "This is a Test Message";

        String messageID = "";
        try {
            messageID = smppSession.submitShortMessage("CMT", sourceTON, sourceNPI, fromNumber,
                    destinationTON, destinationNPI, prefixToNumber + MSISDN,
                    new ESMClass(), (byte) 0, (byte) 0, null, null,
                    new RegisteredDelivery(SMSCDeliveryReceipt.DEFAULT), (byte) 1,
                    new GeneralDataCoding(false, true, MessageClass.CLASS1, Alphabet.ALPHA_DEFAULT), (byte) 0,
                    msgBody.getBytes());
        } catch (PDUException e) {
            //    log.error("Thread[" + nThreadNo + "] -> Failed [" + toNumber+"] : "+"Invalid PDU parameter",e);
            System.err.println("Invalid PDU parameter");
            e.printStackTrace();
        } catch (ResponseTimeoutException e) {
            //    log.error("Thread[" + nThreadNo + "] -> Failed [" + toNumber+"] : "+"Response timeout",e);
            System.err.println("Response timeout");
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            //    log.error("Thread[" + nThreadNo + "] -> Failed [" + toNumber+"] : "+"Receive invalid respose",e);
            System.err.println("Receive invalid respose");
            e.printStackTrace();
        } catch (NegativeResponseException e) {
            //  log.error("Thread[" + nThreadNo + "] -> Failed [" + toNumber+"] : "+"Receive negative response",e);
            System.err.println("Receive negative response");
            e.printStackTrace();
        } catch (IOException e) {
            //   log.error("Thread[" + nThreadNo + "] -> Failed [" + toNumber+"] : "+"IO error occur",e);
            System.err.println("IO error occur");
            e.printStackTrace();
        } catch (Exception e) {
            //   log.error("Thread[" + nThreadNo + "] -> Failed [" + toNumber+"] : "+"Unknown error occur",e);
            System.err.println("Unknown error occur");
            e.printStackTrace();
        } finally {
            System.out.print(messageID);
        }

    }
}

1 comment:

My hollywood Feed said...

Hi, SMPP Server Gateway is widely used by SMS aggregators, enterprises and wholesalers for connecting their software or applications for sending promotional and transactional text messages in bulk across the globe. The SMPP server is also known for supporting different varieties of SMSs including binary, Unicode and concatenated messaging.