Table of Contents

Getting Started with Inetlab.SMPP

Connecting to SMPP server

For connecting to SMPP server you need to get SMPP Account (username and password) from any SMPP provider or mobile network operator.

readonly SmppClient _client = new SmppClient();

public async Task Connect()
{
    if (await _client.ConnectAsync("smpp.server.com", 7777))
    {
        _log.Info("Connected to SMPP server");

        BindResp bindResp = await _client.BindAsync("username", "password", ConnectionMode.Transceiver);

        if (bindResp.Header.Status == CommandStatus.ESME_ROK)
        {
            _log.Info("Bound with SMPP server");
        }
    }
}

In Bind method you can specify ConnectionMode. There are 3 modes you can use:

  • Transmitter - allows only to send SMPP commands to the SMSC and receive corresponding SMPP responses from the SMSC.
  • Receiver - allows only to receive SMPP commands from SMSC and send corresponding SMPP responses.
  • Transceiver - allows to send and receive SMPP commands in SMSC.

Default ConnectionMode is Transceiver.

If you use same SMPP account in several applications you must bind only one application in Receiver or Transceiver connection mode. Other applications should be bound with Transmitter mode. Otherwise SMPP server can deliver messages to an application where you don't expect them.

Sending SMS message

For sending simple SMS message from your service number (short code 1111) to the phone number 79171234567 and with requested delivery receipt you can use fluent PDU builder:

public async Task SendMessage()
{
    IList<SubmitSmResp> responses = await _client.SubmitAsync(
        SMS.ForSubmit()
            .Text("Test Test Test Test Test Test Test Test Test Test")
            .From("1111")
            .To("79171234567")
            .Coding(DataCodings.UCS2)
            .DeliveryReceipt()
    );
}

or the same code, but with explicit instance creation:

public SubmitSm CreateSubmitSm()
{
    SubmitSm sm = new SubmitSm();
    sm.UserData.ShortMessage = _client.EncodingMapper.GetMessageBytes("Test Test Test Test Test Test Test Test Test Test", DataCodings.Default);
    sm.SourceAddress = new SmeAddress("1111", AddressTON.NetworkSpecific, AddressNPI.Unknown);
    sm.DestinationAddress = new SmeAddress("79171234567", AddressTON.Unknown, AddressNPI.ISDN);
    sm.DataCoding = DataCodings.UCS2;
    sm.SMSCReceipt = SMSCDeliveryReceipt.SuccessOrFailure;

    return sm;
}

There is only one difference. First code allows you to send text with any length, but second code only up to 160 symbols. First code returns list of responses because it will split long message text on smaller concatenated parts.

Receiving SMS messages

When client is bound with SMPP server in Transceiver or Receiver mode it will be able to receive SMS messages. Every time when SMPP client receives SMS message it raises evDeliverSm event. Event handler method should be attached before bind to server.

_client.evDeliverSm += new DeliverSmEventHandler(client_evDeliverSm);

Your code should be able to receive concatenated long SMS messages. Message Composer helps to combine them and get full message text. Delivery Receipt can also be received with this handler method.

private readonly MessageComposer _composer = new MessageComposer();

private void client_evDeliverSm(object sender, DeliverSm data)
{
    try
    {
        //Check if we received Delivery Receipt
        if (data.MessageType == MessageTypes.SMSCDeliveryReceipt)
        {
            //Get MessageId of delivered message
            string messageId = data.Receipt.MessageId;
            MessageState deliveryStatus = data.Receipt.State;
        }
        else
        {
            // Receive incoming message and try to concatenate all parts
            if (data.Concatenation != null)
            {
                _composer.AddMessage(data);

                _log.Info("DeliverSm part received : Sequence: {0} SourceAddr: {1} Concatenation ( {2} ) Coding: {3} Text: {4}",
                    data.Header.Sequence, data.SourceAddress, data.Concatenation, data.DataCoding, _client.EncodingMapper.GetMessageText(data));


                if (_composer.IsLastSegment(data))
                {
                    string fullMessage = _composer.GetFullMessage(data);
                    _log.Info("Full message: " + fullMessage);
                }
            }
            else
            {
                _log.Info("DeliverSm received : Sequence: {0} SourceAddr : {1} Coding : {2} MessageText : {3}",
                        data.Header.Sequence, data.SourceAddress, data.DataCoding, _client.EncodingMapper.GetMessageText(data));
            }
        }
    }
    catch (Exception ex)
    {
        data.Response.Header.Status = CommandStatus.ESME_RX_T_APPN;
        _log.Error("Failed to process DeliverSm", ex);
    }

Possible reasons why you don't receive incoming messages:

  • SMPP account doesn't have right to receive SMS messages.
  • Wrong SMS routing configuration on SMPP server.
  • SMPP client has been bound as Transmitter.
  • SMPP client was not attached to evDeliverSm event handler.
  • SMPP account is used in two or more application. SMSC sends messages to application where DeliverSm is not expected.