Sending SMS in C# (.NET): Overview of Libraries and APIs
In modern C# (.NET) development, there is often a need to integrate SMS notifications: registration confirmation, two-factor authentication, order status alerts, or simply mass mailing. In this article, we will cover three popular approaches to sending SMS from C# applications: using cloud services Twilio, Vonage (formerly Nexmo), and the Russian service SMSC.ru. All code examples are written for .NET 6/8 and use modern asynchronous methods.
1. Sending SMS via Twilio (global provider)
Twilio is one of the most popular services for sending SMS, voice messages, and other communications. It has excellent documentation and an official NuGet package.
Installing the library
Add the package via NuGet Package Manager or run the command:
dotnet add package TwilioExample of sending SMS in C#
using Twilio;using Twilio.Rest.Api.V2010.Account;using Twilio.Types;
public class SmsService{ // Specify Account SID and Auth Token from the Twilio console private readonly string _accountSid = "YOUR_ACCOUNT_SID"; private readonly string _authToken = "YOUR_AUTH_TOKEN"; private readonly string _fromPhone = "+1234567890"; // Number purchased in Twilio
public async Task<bool> SendSmsAsync(string toPhone, string message) { TwilioClient.Init(_accountSid, _authToken);
try { var msg = await MessageResource.CreateAsync( body: message, from: new PhoneNumber(_fromPhone), to: new PhoneNumber(toPhone) ); Console.WriteLine($"SMS sent! SID: {msg.Sid}"); return true; } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}"); return false; } }}Important: The sender number must be verified in your Twilio account. For trial accounts, SMS are only sent to verified numbers.
2. Sending SMS via Vonage (formerly Nexmo)
Vonage is another powerful cloud provider. Its API is simple, and the package is easily installed via NuGet.
Installing the library
dotnet add package VonageExample of sending SMS
using Vonage;using Vonage.Request;using Vonage.Messaging;
public class VonageSmsService{ private readonly string _apiKey = "YOUR_API_KEY"; private readonly string _apiSecret = "YOUR_API_SECRET"; private readonly string _from = "YourApp"; // Sender name
public async Task<bool> SendSmsAsync(string toPhone, string message) { var credentials = Credentials.FromApiKeyAndSecret(_apiKey, _apiSecret); var client = new VonageClient(credentials);
var request = new SendSmsRequest { To = toPhone, From = _from, Text = message };
var response = await client.SmsClient.SendAnSmsAsync(request);
if (response.Messages[0].Status == "0") { Console.WriteLine($"SMS sent! ID: {response.Messages[0].MessageId}"); return true; } else { Console.WriteLine($"Error: {response.Messages[0].ErrorText}"); return false; } }}Note: Sending to Russian numbers may require additional configuration of the alpha name.
3. Sending SMS via SMSC.ru (Russian service)
If your audience is primarily in Russia and the CIS, SMSC.ru may be a more convenient and cheaper option. No library is required — just an HTTP request.