> For the complete documentation index, see [llms.txt](https://developer.ecall-messaging.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.ecall-messaging.com/api-references/smpp/code-examples.md).

# Code examples

### Setup and dependencies

{% tabs %}
{% tab title="Python" %}

```bash
pip install smpplib
```

{% endtab %}

{% tab title="Java" %}

```xml
<dependency>
    <groupId>com.cloudhopper</groupId>
    <artifactId>ch-smpp</artifactId>
    <version>5.0.8</version>
</dependency>
```

{% endtab %}

{% tab title="C# (.NET)" %}

```bash
dotnet add package ArdaXi.SmppSharp
```

{% endtab %}
{% endtabs %}

### Connect and send SMS

{% tabs %}
{% tab title="Python" %}

```python
import smpplib
import smpplib.client
import smpplib.consts

client = smpplib.client.Client("smpp.ecall.ch", 2776, allow_unknown_opt_params=True)

client.connect()
client.bind_transceiver(system_id="your_username", password="your_password")

pdu = client.send_message(
    source_addr_ton=smpplib.consts.SMPP_TON_INTL,
    source_addr_npi=smpplib.consts.SMPP_NPI_ISDN,
    source_addr="eCall",
    dest_addr_ton=smpplib.consts.SMPP_TON_INTL,
    dest_addr_npi=smpplib.consts.SMPP_NPI_ISDN,
    destination_addr="0041791234567",
    short_message="Hello from eCall SMPP!".encode("utf-8"),
    registered_delivery=1,
)

print(f"Message ID: {pdu.message_id}")

client.unbind()
client.disconnect()
```

{% endtab %}

{% tab title="Java" %}

```java
import com.cloudhopper.smpp.*;
import com.cloudhopper.smpp.pdu.*;
import com.cloudhopper.smpp.type.*;

SmppSessionConfiguration config = new SmppSessionConfiguration();
config.setHost("smpp.ecall.ch");
config.setPort(2776);
config.setSystemId("your_username");
config.setPassword("your_password");
config.setType(SmppBindType.TRANSCEIVER);
config.setUseSsl(true);

DefaultSmppClient smppClient = new DefaultSmppClient();
SmppSession session = smppClient.bind(config);

SubmitSm request = new SubmitSm();
request.setSourceAddress(new Address((byte) 0x05, (byte) 0x00, "eCall"));
request.setDestAddress(new Address((byte) 0x01, (byte) 0x01, "0041791234567"));
request.setShortMessage("Hello from eCall SMPP!".getBytes("UTF-8"));
request.setRegisteredDelivery((byte) 0x01);

SubmitSmResp response = session.submit(request, 10000);
System.out.println("Message ID: " + response.getMessageId());

session.unbind(5000);
session.destroy();
```

{% endtab %}

{% tab title="C# (.NET)" %}

```csharp
using JamaaTech.Smpp.Net.Client;
using JamaaTech.Smpp.Net.Lib;

var settings = new SmppConnectionSettings
{
    Host = "smpp.ecall.ch",
    Port = 2776,
    SystemID = "your_username",
    Password = "your_password",
    SystemType = "",
    InterfaceVersion = 0x34,
    UseSsl = true
};

using var client = new SmppClient();
client.Properties = settings;
client.Connect();

var msg = new TextMessage
{
    SourceAddress = "eCall",
    DestinationAddress = "0041791234567",
    Text = "Hello from eCall SMPP!",
    RegisterDeliveryNotification = true
};

client.SendMessage(msg);
client.Disconnect();
```

{% endtab %}
{% endtabs %}

### Receive delivery receipts

{% tabs %}
{% tab title="Python" %}

```python
import smpplib
import smpplib.client

def message_received(pdu):
    if hasattr(pdu, "esm_class") and pdu.esm_class == 0x04:
        print(f"Delivery receipt: {pdu.short_message}")
    else:
        print(f"Inbound SMS from {pdu.source_addr}: {pdu.short_message}")

client = smpplib.client.Client("smpp.ecall.ch", 2776)
client.connect()
client.bind_transceiver(system_id="your_username", password="your_password")

client.set_message_received_handler(message_received)
client.listen()
```

{% endtab %}

{% tab title="Java" %}

```java
import com.cloudhopper.smpp.*;
import com.cloudhopper.smpp.pdu.*;

public class ECallSmppHandler extends DefaultSmppSessionHandler {

    @Override
    public PduResponse firePduRequestReceived(PduRequest pduRequest) {
        if (pduRequest instanceof DeliverSm) {
            DeliverSm deliver = (DeliverSm) pduRequest;
            byte esmClass = deliver.getEsmClass();

            if (esmClass == 0x04) {
                System.out.println("Delivery receipt: " +
                    new String(deliver.getShortMessage()));
            } else {
                System.out.println("Inbound SMS from " +
                    deliver.getSourceAddress().getAddress() + ": " +
                    new String(deliver.getShortMessage()));
            }
        }
        return pduRequest.createResponse();
    }
}
```

{% endtab %}
{% endtabs %}

### Keep-alive

{% tabs %}
{% tab title="Python" %}

```python
import threading
import time

def keep_alive(client, interval=30):
    while True:
        time.sleep(interval)
        try:
            client.enquire_link()
        except Exception:
            break

thread = threading.Thread(target=keep_alive, args=(client,), daemon=True)
thread.start()
```

{% endtab %}

{% tab title="Java" %}

```java
config.setRequestExpiryTimeout(30000);
config.setWindowMonitorInterval(15000);
```

{% endtab %}
{% endtabs %}
