DEV Community

Cover image for Sending SMS with Twilio and .NET
Sean Drew
Sean Drew

Posted on • Edited on

Sending SMS with Twilio and .NET

Twilio is a powerful cloud communications platform that allows developers to easily integrate communication services like SMS messaging, voice calls, video conferencing, and email into their applications. By providing developer-friendly APIs, Twilio eliminates the need to build or maintain complex telecom infrastructure. This guide demonstrates how to send an SMS message using Twilio in a .NET application.

Setting Up Twilio in Your .NET Application
To get started with Twilio, you’ll need the following:

  • A Twilio account with access to your Account SID and Auth Token.
  • A verified Twilio phone number to send messages.

Below is a simple example of how to send an SMS using Twilio's .NET library:

Example Implementation

using System;  
using System.Threading.Tasks;  
using Twilio;  
using Twilio.Rest.Api.V2010.Account;  
using Twilio.Types;  

public class SmsExample  
{  
    private const string AccountSid = "your_account_sid";  
    private const string AuthToken = "your_auth_token";  
    private const string FromPhoneNumber = "your_twilio_phone_number";  

    public static async Task Main(string[] args)  
    {  
        // Initialize Twilio client with Account SID and Auth Token  
        TwilioClient.Init(AccountSid, AuthToken);  

        // Define recipient phone number and message body  
        var toPhoneNumber = new PhoneNumber("recipient_phone_number");  
        var messageBody = "Hello! This is a message from your .NET application.";  

        // Create message options  
        var messageOptions = new CreateMessageOptions(toPhoneNumber)  
        {  
            From = new PhoneNumber(FromPhoneNumber),  
            Body = messageBody  
        };  

        // Send the SMS  
        var message = await MessageResource.CreateAsync(messageOptions);  

        // Output the sent message SID  
        Console.WriteLine($"Message sent with SID: {message.Sid}");  
    }  
}  

Enter fullscreen mode Exit fullscreen mode

Explanation of Key Components
Twilio Initialization
The TwilioClient.Init(AccountSid, AuthToken); call initializes the Twilio client with your account credentials. This is required to authenticate API calls.

Message Configuration
A CreateMessageOptions object defines the recipient (To), sender (From), and content (Body) of the SMS.

Sending the Message
MessageResource.CreateAsync(messageOptions) sends the SMS asynchronously and returns the message details, including the unique SID, which can be used to track the status of the message.

Why Use Twilio?
Ease of Integration: Twilio provides SDKs for multiple programming languages, including .NET, making it simple to implement communication features.

Scalability: Twilio handles all the telecom infrastructure, allowing your application to scale without worrying about message delivery.

Global Reach: With support for over 180 countries, Twilio is an excellent choice for applications with an international audience.

Conclusion
Sending SMS messages with Twilio is quick and straightforward, thanks to its developer-friendly API. By integrating Twilio into your .NET application, you can easily add robust messaging capabilities without dealing with the complexity of telecom systems. With just a few lines of code, your application can send SMS notifications, alerts, or confirmations to users worldwide.

http://www.seandrew.info - https://www.linkedin.com/in/seanmdrew

Top comments (0)