Short Message Peer-to-Peer (SMPP) is an open telecommunications industry protocol for exchanging text messages (SMS) between short message service centers (SMSCs) and External Short Messaging Entities (ESMEs). The protocol is often used to connect to SMS centers to send messages to mobile devices.
OpenSMPP is an open-source Java library that provides an API for developing ESMEs and Messaging Gateways. This post contains code snippets demonstrating the most common operations using 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();
}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();
}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;
}Unbinding
Log out from the SMSC and close the connection:
try {
log.info("Send unbind request...");
final UnbindResp response = smppSession.unbind();
log.info("Unbind response " + response.debugString());
} catch (Throwable e) {
e.printStackTrace();
}These four examples must be sufficient 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 the SMSC
- close the connection before the application shuts down
