Table of Contents

Deliver messages from sender to recipient

By combining receive message and send message examples we can implement basic way to deliver messages from sender to recipient via SMPP-server. Let’s make a method for receiving inbound messages from an SMPP-client, searching suitable recipient among SMPP-clients connected and sending the message to it. For the sake of example, let us consider any SMPP-client having SystemID (login) equal to message "To" field as "suitable".

private static async Task Main(string[] args)
{
    LogManager.SetLoggerFactory(new ConsoleLogFactory(LogLevel.Verbose));

    SmppServer _server = new SmppServer(new IPEndPoint(IPAddress.Any, 7777));

    _server.evClientBind += (s, c, p) => { }; //allow all to authenticate on the server
    _server.evClientSubmitSm += async (smppServer, smppServerClient, submitSm) => await ForwardSms(smppServer, smppServerClient, submitSm);

    await _server.StartAsync();

    Console.ReadLine();
}

private static async Task ForwardSms(object smppServer, SmppServerClient smppServerClient, SubmitSm submitSm)
{
    SmppServer _server = (SmppServer)smppServer;

    //prepare message
    string fromField = submitSm.SourceAddress.ToString();
    string toField = submitSm.DestinationAddress.ToString();
    string textField = submitSm.GetMessageText(smppServerClient.EncodingMapper);

    //search recepient
    SmppServerClient clientRecepient = _server.ConnectedClients.FirstOrDefault(c => c.SystemID == toField);

    //send
    if (clientRecepient != null)
    {
        IList<DeliverSm> textMessage = SMS.ForDeliver().From(fromField).To(toField).Text(textField).Create(clientRecepient);
        var result = await clientRecepient.DeliverAsync(textMessage);
    }
}

There is an event handler created and named “ForwardSms”. It is subscribed to evClientSubmitSm event, responsible for inbound messages. The event handler is getting the inbound message as a third argument (submitSm) and prepares it for sending further. In addition, it picks the suitable recipient out of the list of SMPP-clients connected (ConnectedClients property) and forwards message to it.

To test the setup, connect two SMPP-clients (sender and recipient) to this SMPP-server. Sender login is not important, but for recipient login use “client002”. Now send a message from the first SMPP-client and put “client002” in the “Destination (To)” field. The message should arrive to client with login “client002”. It will work even if you have only one SMPP-client connected but "To" field contains its login. Of course, the way of choosing the recipient is totally up to the developer.

Using a similar approach it is possible to implement the SMPP Gateway for forwarding messages via SMPP-client connected to another SMPP-server.