Sending Multipart Messages with the SMPP Component


A single SMS message can hold at most 140 bytes of user data. Longer messages must be split into multiple parts, which the recipient's phone then reassembles and displays as one message.

Sending Multipart Messages

The SMPP component splits long messages automatically when the SplitLargeMessages configuration setting is set to true. When SendMessage is called with a message that is larger than the MaxSMSLength configuration setting (140 bytes by default), the component splits it into parts and sends each part to the recipients.

SMPP smpp = new SMPP();
smpp.SMPPServer = "smpp.example.com";
smpp.SMPPPort = 2775;
smpp.UserId = "user";
smpp.Password = "password";
smpp.Connect();
smpp.AddRecipient(0, "15555550123");
smpp.Config("SplitLargeMessages=true");
string message = "This is a long message that is split into several smaller parts for transmission, and then reassembled into a single message on the recipient's phone. The number of characters in each part depends on the data coding used.";
// When the message is split, the IDs of the parts are separated by spaces.
string messageIds = smpp.SendMessage(message);
Console.WriteLine("Message IDs: " + messageIds);
smpp.Disconnect();

When a message is split, the value returned by SendMessage (and the MessageId property) holds the server-assigned ID of each part, separated by spaces. A message can be split into at most 255 parts.

Split Methods

The SplitMessageMethod configuration setting determines how the parts are marked so that the recipient can reassemble them:

Value Method Description
0 (default) UDH A 6-byte user data header (UDH) is added to the beginning of each part, which holds a reference number, the total number of parts, and the number of the part. The component sets the UDH indicator flag automatically.
1 SAR The segmentation and reassembly (SAR) optional parameters (sar_msg_ref_num, sar_total_segments, and sar_segment_seqnum) are sent with each part instead of a UDH.

The UDH method is supported by most servers and is the most common choice. The SAR method should be used only when the server requires it.

Splitting is supported only on GSM networks.

Characters per Part

The number of characters in each part depends on how the message is encoded. With the UDH method, 6 of the 140 bytes are used by the header, which leaves 134 bytes for the message text:

Encoding Configuration Characters per Part (UDH)
GSM 7-bit UseGSM7BitEncoding=true 153
8-bit (default) None 134
UCS2 (Unicode) DataCoding=8 67

To send characters that are not supported by the GSM 7-bit alphabet (such as most non-Latin scripts and emoji), use UCS2 encoding.

smpp.Config("DataCoding=8"); // Use UCS2 encoding.
smpp.Config("SplitLargeMessages=true");
smpp.SendMessage(message);

Receiving Multipart Messages

When a part of a multipart message is received, the MessageIn event fires once for each part. The MessagePartCount parameter holds the total number of parts, the MessagePart parameter holds the number of the current part (from 1 to MessagePartCount), and the MessagePartReference parameter holds a reference value that is the same for all parts of the message. Since the parts may arrive in any order, the application should collect the parts by reference and combine them in order once all parts have arrived.

Dictionary<int, string[]> pending = new Dictionary<int, string[]>();
smpp.OnMessageIn += (s, e) =>
{
  if (e.MessagePartCount <= 1)
  {
    Console.WriteLine("Message: " + e.Message);
    return;
  }
  // Store the part, and combine the parts once all of them have been received.
  if (!pending.TryGetValue(e.MessagePartReference, out string[] parts))
  {
    parts = new string[e.MessagePartCount];
    pending[e.MessagePartReference] = parts;
  }
  parts[e.MessagePart - 1] = e.Message;
  if (Array.TrueForAll(parts, p => p != null))
  {
    pending.Remove(e.MessagePartReference);
    Console.WriteLine("Message: " + string.Concat(parts));
  }
};

Note that in older versions of IPWorks, multipart messages were sent by building the UDH manually and sending each part with the HexString configuration setting. In current versions, the SplitLargeMessages configuration setting handles this automatically.

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.