Short‑Message Service (SMS) is still the most widely supported mobile‑text channel worldwide. The following guide covers how to send, receive and manage SMS traffic from a C# codebase. It covers the core concepts (protocols, libraries and patterns), security/compliance concerns, typical use‑cases and the most common pitfalls.
SMS Fundamentals
What is SMS?
- Legacy protocol: the text is transmitted via the carrier’s SMSC.
- Maximum 160 characters (GSM‑7). Longer texts are split into parts.
- Delivered even on low‑bandwidth or isolated networks.
How SMS Reaches the Recipient
- Your app talks to a gateway (SMPP, HTTP or AT‑command).
- Gateway binds to the carrier’s SMSC.
- Message is forwarded to the destination handset.
- Delivery receipt is routed back via the same channel.
Protocols – The Core Building Blocks
SMPP (Short Message Peer‑to‑Peer)
Low‑latency, high‑throughput, but requires dedicated TCP connection and credentials.
HTTP/REST API
Most cloud SMS providers expose HTTPS endpoints. The C# HttpClient is usually enough, but consider HttpClientFactory in ASP.NET Core to avoid socket exhaustion.
AT Commands over Serial
Control a GSM modem or mobile phone directly. Free of charge for low volume but limited by physical connectivity.
Libraries & Frameworks
Commercial SDKs
- Wrap carrier APIs (e.g., Twilio, Nexmo).
- Provide helpers for 2FA, bulk messaging, retries and analytics.
- Follow .NET Standard, so they run on .NET Core, .NET 5+, Xamarin and Unity.
Open‑Source Libraries
SmppClient.NET– SMPP protocol stack.SmsHttpClient– lightweight wrapper for JSON‑based APIs.- Both expose configuration objects for host, port, system ID, password (SMPP) or API key (HTTP).
Serial & Socket APIs
Use System.IO.Ports.SerialPort for AT commands, System.Net.Sockets for raw TCP/UDP. Suitable for custom or embedded deployments.
Implementation Patterns
Using SMPP
- Configure gateway:
host, port, systemId, password. - Create
SmpPClient, bind withbind_sm. - Send
submit_smmessages. - Receive
deliver_smfor receipts. - Gracefully unbind on shutdown.
HTTP/REST API
- POST to
/messageswith JSON body:{to, from, message, scheduletime, callbackurl}. - Authenticate via API key in
Authorizationheader. - Use
HttpClientFactoryfor efficient connection reuse. - Implement exponential back‑off on failures and respect provider TPS limits.
- Handle webhook callbacks for delivery reports.
Serial AT Commands
- Open serial port at modem’s baud rate.
- Set SMS mode:
AT+CMGF=1(text). - Set character set:
AT+CSCS="GSM". - Send message:
AT+CMGS=""+.Message - Wait for
+CMGS:response.
Cloud Managed Services
- Automatic carrier selection, routing and compliance.
- Cost: per‑message fee plus optional monthly usage tiers.
- Benefits: scale without server maintenance, rich analytics, built‑in templates.
- Drawbacks: vendor lock‑in, higher cost at large volumes.
Security & Compliance
Transport Encryption
- SMPP: wrap TCP with TLS (e.g.,
SslStream). - HTTP: use
https://endpoints. - Serial: secure via VPN or isolated network.
Regulatory Requirements
- TCPA (US) – opt‑in, opt‑out, caller ID.
- GDPR (EU) – data minimisation, consent logging.
- Record opt‑out lists and message metadata for audit.
Rate‑Limiting
Apply a token bucket algorithm on the client side. Most libraries expose MaxTps or MaxMessagesPerMinute settings.
Message Reliability
Not all carriers provide timely delivery receipts. For critical flows, add a second polling endpoint or use an alternative channel (e.g., push notification) as a fallback.
Typical Use‑Cases
- 2FA & OTPs – One‑time codes, short texts, quick delivery.
- Transactional alerts – bank balances, OTPs, password resets.
- Bulk marketing – carefully split, cost‑aware, compliance‑ready.
- Notifications for IoT & remote devices (e.g., device status updates).
Common Pitfalls & Mitigations
- Message fragmentation – each part costs extra. Use
+CSQto split or switch to PDU mode. - Missing receipts – fallback to status query API if webhook fails.
- High latency with SMPP – use connection pooling and keep‑alive.
- Unexpected carrier throttling – test against the carrier’s spec and adjust TPS.
Future Directions
Emerging standards such as SMPP over WebSocket and integration of MQTT with SMS gateways are being prototyped. However, for most business scenarios SMS remains the most robust, globally‑available channel. The key to success is to pick the right protocol, use a battle‑tested library, enforce TLS, observe legal constraints, and structure the code for scalability.
No comments yet. Be the first to comment!