Conference Campaign Solo Rich Messaging

Tutorial section - SMS

Send SMS with SMPP using Java

Sending SMS with the SMPP API using the Java programming language

The jsmpp library can be used to make SMPP requests using Java. This library's higher-level functions can be used to synchronously submit SMS to an SMS gateway or SMSC.

SMPP requirements and resources

SMPP is the Short Message Peer-to-Peer protocol for conveying SMS operations. An SMPP account can easily be obtained for using the Tyr SMS Gateway or SMSC Simulator. The following are required to send SMS with SMPP:

  • SMPP API account - SMPP API account for the Melrose Labs Tyr SMS Gateway
  • SMPP API account (simulator) - SMPP API account for the Melrose Labs SMSC Simulator. Allows you to simulate inbound and outbound SMS delivery using an SMPP API account with your application.
  • SMPP Protocol [reference] - Short Message Peer-to-Peer Protocol v3.3, v3.4 and v5 specifications and guides

Java requirements and resources

Java is a programming language and can be used to quickly and easily add SMS support for programmatically sending and receiving SMS messages. Use it for transactional messaging and notifications between your application and mobiles. The following are required to send SMS using Java:

  • Java - Java programming language

Requirements and resources

The following are required to send SMS with SMPP using Java:

  • jSMPP - jSMPP Java Library

Code

Create the file SimpleSubmitExample.java containing the code below. Replace the hostname smscsim.melroselabs.com, port 2775, SYSTEMID and PASSWORD values with those from your SMPP API account. Alternatively, replace SYSTEMID and PASSWORD with those allocated to you for use with the Melrose Labs SMSC Simulator.

The following example Java code opens an SMPP transmitter bind to smscsim.melroselabs.com on port 2775 (SMPP port), and then sends the message Hello World #$£ to mobile number 447712345678 from MelroseLabs. The message ID returned in messageId is that returned from the SMS gateway or SMSC after a successful submission. The SMPP system ID and password for the SMPP account are contained in SYSTEMID and PASSWORD respectively.

	import logging
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleSubmitExample {
    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleSubmitExample.class);
    
    public static void main(String[] args) {
        SMPPSession session = new SMPPSession();
        try {
            LOGGER.info("Connecting");
            String systemId = session.connectAndBind("smscsim.melroselabs.com", 2775, new BindParameter(BindType.BIND_TX, "SYSTEMID", "PASSWORD", "", TypeOfNumber.UNKNOWN, NumberingPlanIndicator.UNKNOWN, null));
            
            try {
                String messageId = session.submitShortMessage("",
                    TypeOfNumber.UNKNOWN, NumberingPlanIndicator.UNKNOWN, "MelroseLabs",
                    TypeOfNumber.INTERNATIONAL, NumberingPlanIndicator.UNKNOWN, "447712345678",
                    new ESMClass(), (byte)0, (byte)1,  TIME_FORMATTER.format(new Date()), null,
                    new RegisteredDelivery(SMSCDeliveryReceipt.DEFAULT), (byte)0, new GeneralDataCoding(Alphabet.ALPHA_DEFAULT, MessageClass.CLASS1, false), (byte)0,
                    "Hello World €$£".getBytes());
                LOGGER.info("Message successfully submitted (message_id={})", messageId);
            } catch (PDUException e) {
                // Invalid PDU parameter
                LOGGER.error("Invalid PDU parameter", e);
            } catch (ResponseTimeoutException e) {
                // Response timeout
                LOGGER.error("Response timeout", e);
            } catch (InvalidResponseException e) {
                // Invalid response
                LOGGER.error("Receive invalid response", e);
            } catch (NegativeResponseException e) {
                // Receiving negative response (non-zero command_status)
                LOGGER.error("Receive negative response, e");
            } catch (IOException e) {
                LOGGER.error("IO error occured", e);
            }

            session.unbindAndClose();

        } catch (IOException e) {
            LOGGER.error("Failed connect and bind to SMSC", e);
        }
    }
}

Other APIs covered in our tutorials that can be used for sending and receiving SMS using Java include: REST


Whatever the language or API, you can send SMS and receive SMS between applications and mobiles for a wide range of uses with a number of our services. Please consider using our Messaging, SMS gateway and Bulk SMS solutions. If you are interested in testing your application's SMS support and are using the SMPP protocol, then we recommend that you take a look at the Melrose Labs SMSC Simulator service that can simulate SMS message delivery to and from mobiles. The SMSC Simulator supports SMPP v3.3, v3.4 and v5, and has a high degree of SMPP protocol conformance. Later in 2020 we will be providing access to the Melrose Labs Tyr SMS Gateway for A2P, P2A, bulk, wholesale and business SMS, and for text marketing and other applications. The Melrose Labs Tyr SMS Gateway will support REST and SMPP APIs.

Follow us on LinkedIn for updates on Melrose Labs services and news. Follow Us