Sending SMS in C# (.NET): Integration with SMS Service APIs
In the modern world, sending SMS messages remains a popular task for notifications, two-factor authentication, and customer alerts. The .NET ecosystem does not have a built-in library for sending SMS — all operations are performed via HTTP requests to the APIs of third-party SMS providers. In this article, we will explore how to organize SMS sending in C# using HttpClient and look at integration examples with popular services.
Choosing an SMS Provider and Key Parameters
Before starting, you need to register with one of the SMS services. The most popular ones are: Twilio, Vonage (Nexmo), SMS Aero, SMS Center. All of them provide a REST API. To send messages, you will need:
- API key / token — a unique identifier for your account.
- Secret / password — a secret key for authentication.
- Sender ID — the string that will be displayed to the recipient (e.g., your brand).
- Recipient number — in international format (e.g., +71234567890).
Basic Implementation in C# with HttpClient
For working with HTTP requests in .NET, the System.Net.Http.HttpClient class is used. Below is an example of sending an SMS via the Twilio API (one of the most popular services).
using System;using System.Net.Http;using System.Text;using System.Threading.Tasks;
public class SmsSender{ private readonly HttpClient _httpClient; private readonly string _accountSid; private readonly string _authToken;
public SmsSender(string accountSid, string authToken) { _accountSid = accountSid; _authToken = authToken; _httpClient = new HttpClient(); }
public async Task SendSmsAsync(string to, string from, string message) { // URL for Twilio API string url = $"https://api.twilio.com/2010-04-01/Accounts/{_accountSid}/Messages.json";
// Form the form data var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("To", to), new KeyValuePair<string, string>("From", from), new KeyValuePair<string, string>("Body", message) });
// Add Basic authentication string auth = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{_accountSid}:{_authToken}")); _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", auth);
// Send POST request HttpResponseMessage response = await _httpClient.PostAsync(url, content);
if (response.IsSuccessStatusCode) { string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine($"SMS sent successfully: {responseBody}"); } else { string error = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Error: {response.StatusCode} - {error}"); } }}Using the Class
class Program{ static async Task Main(string[] args) { // Replace with your data from the Twilio dashboard string accountSid = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; string authToken = "your_auth_token_here";
var sender = new SmsSender(accountSid, authToken); await sender.SendSmsAsync( to: "+71234567890", from: "+15017122661", // number allocated by Twilio message: "Hello! This is a test SMS from C#." ); }}Alternative Providers: Example with Vonage (Nexmo)
For the Vonage API, a different form