Building ESME with Logica OpenSMPP Java library

Short Message Peer to Peer (SMPP) is an open, telecommunications industry protocol for exchanging text messages (SMS) between short message service centers (SMSC) and External Short Messaging Entities (ESMEs). The protocol is often used for connecting to SMS centers in order to send messages to mobile devices.

OpenSMPP is an open source Java library designed to provide an API for developing ESMEs and Messaging Gateways. This post contains code snippets that demonstrate the most common operations that can be done with the help of the OpenSMPP library.

Binding

Connect your application to SMSC by sending a bind request:

try {
    final BindRequest request = new BindTransmitter();
    request.setSystemId(smscUsername);
    request.setPassword(smscPassword);
    request.setSystemType(systemType);
    request.setAddressRange(addressRange);
    request.setInterfaceVersion((byte) 0x34);
    final TCPIPConnection connection = 
        new TCPIPConnection(smscHost, smscPort);
    connection.setReceiveTimeout(BIND_TIMEOUT);
    session = new Session(connection);
    log.info("Send bind request...");
    final BindResponse response = session.bind(request);
    log.info("Bind response " + response.debugString());
} catch (Throwable e) {
    e.printStackTrace();  
}Code language: PHP (php)

Sending an enquire link

Check the alive status of the other party (can be sent both by SMSC and ESME):

try {
    final EnquireLink request = new EnquireLink();
    log.info("Enquire Link request " + request.debugString());
    final EnquireLinkResp response = session.enquireLink(request);
    log.info("Enquire Link response " + response.debugString());
} catch (Throwable e) {
    e.printStackTrace();
}Code language: PHP (php)

Sending an SMS message

Send an SMS message through an SMSC:

try {
    final SubmitSM request = new SubmitSM();
    request.setServiceType(serviceType);
    request.setSourceAddr(createAddress(senderPhoneNumber));
    request.setDestAddr(createAddress(recipientPhoneNumber);
    request.setShortMessage(messageText);
    request.setScheduleDeliveryTime(deliveryTime);
    request.setReplaceIfPresentFlag((byte) 0);
    request.setEsmClass((byte) 0);
    request.setProtocolId((byte) 0);
    request.setPriorityFlag((byte) 0);
    request.setRegisteredDelivery((byte) 0);
    request.setDataCoding((byte) 0);
    request.setSmDefaultMsgId((byte) 0);
    final SubmitSMResp response = session.submit(request);
    log.info("Submit response " + response.debugString() + 
        ", message id " + response.getMessageId());
} catch (Throwable e) {
    e.printStackTrace();   
}
private static Address createAddress(String address) 
        throws WrongLengthOfStringException {
    Address addressInst = new Address();
    addressInst.setTon((byte) 1); // national ton
    addressInst.setNpi((byte) 1); // numeric plan indicator
    addressInst.setAddress(address, Data.SM_ADDR_LEN);
    return addressInst;
}Code language: PHP (php)

Unbinding

Log out from the SMSC and clos the connection:

try {
    log.info("Send unbind request...");
    final UnbindResp response = smppSession.unbind();
    log.info("Unbind response " + response.debugString());
} catch (Throwable e) {
    e.printStackTrace();
}Code language: PHP (php)

This four examples must be sufficient enough to create a very basic ESME. The usual flow is to:

  • create a connection by sending a bind request
  • periodically check the alive status with enquire links, and restore the connection in case of a failure
  • perform the actual work by sending SMS messages to SMSC
  • close connection before the application shutdown
Software Developer & Technical Lead