DEV Community

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

Posted on • Edited on

1

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.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay