Send SMS Messages In C# And .NET Using Twilio

Introduction

In modern web applications, the business may require to send SMS to end-users in various scopes like Alerting the users, acknowledging the users, or even sending OTP. Sending as SMS is the easiest way and also can be implemented easily in .NET applications using Twilio.

Setting Up Twilio Account

Twilio two-way SMS and MMS messages allow you to carry on a conversation by both sending and receiving text and multimedia messages. This can enable a contact center agent to engage in an SMS-based chat with a customer, or let customers re-schedule appointments via text and receive automated responses.

Fig. 1 Signup for Twilio Free account
  • Choose the Service which we want like SMS, WhatsApp, Voice and etc from the option.
Fig. 2 Choose the Service which we want – For us, SMS is required
Fig. 3 Choose the Services from the option
  • Get a Virtual Twilio Phone Number to Send & Recieve SMS
Fig. 4 Get a Virtual Twilio Phone Number (Image Source: Twilio Site)

To send or receive an SMS with Twilio, you will need a virtual phone number from Twilio. A virtual phone number is a standard telephone number that is not locked down to a specific phone. It can route a voice call or text message to any phone or application workflow. In addition, you will need a Twilio Account SID and Auth token to connect Twilio with your application.”

Sending SMS in.NET Application

For this article example, let’s create a simple Blazor application that has one UI with two fields, one is to enter the Receiver Phone Number and another is for Message Content.

Choose Visual Studio -> Create a new Project -> Blazor WebAssembly APP

Install the required Twilio NuGet package, for that navigate to Tools and search Twilio.

Fig. 4 Install the Twilio NuGet Package

“Twilio’s NuGet package for .NET helps you to create a new instance of the Message resource, specifying the To, From, and Body parameters of your message. Let’s create a new .NET project and add the Twilio NuGet Package.”

C# Code Implementation

Declare variables for Phone Number, Message Content, Twilio Account SID, and Twilio Auth Token.

public string MessageContent { get; set; }
public string PhoneNumber { get; set; }
public string accountSid = "Paste your Account SID from the Twilio Account";
public string authToken = "Paste your Auth Token from the Twilio Account";
public string TwilioSenderNumber = "Paste your Twilio Phone Number from the Twilio Account";

Create a method which is to send SMS,

protected void SendMessage()
        {
            TwilioClient.Init(accountSid, authToken);
            var message = MessageResource.Create(
                                                    body: MessageContent,
                                                    from: new Twilio.Types.PhoneNumber(TwilioSenderNumber),
                                                    to: new Twilio.Types.PhoneNumber(PhoneNumber)
                                                );

        }

Now run the application and enter the receiver’s phone number and message content.

Fig. 5 Sending SMS using .NET Application (Blazor)

Now the SMS is received by the receiver and it will be as,

Fig. 6 SMS Received by the Receiver

Conclusion

In this simple article, we discussed how to send SMS using Twilio in C# and the .NET applications. We will discuss more on this in upcoming articles. I hope you all liked this article and please feel free to share your feedback in the comment section.

Article References and Credits

The Italic Paragraphs are taken as it is from the official Twilio site.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Website Built with WordPress.com.

Up ↑

%d bloggers like this: