How to Send and Receive SMS Messages Using C# and ASP.NET


This C# SMS Quickstart covers both inbound and outbound SMS. We’re going to use the code from a Guide found in the Twilio Docs. You can follow along from the link down in the description. For everything we’re going to do we’re going to need a phone number so let’s go get one. We’ll start in the Phone number console and I’ll search for a 267 area code number and I’ll buy the first Philadelphia area number I find. I’ll click Buy This Number, and then Setup Number. We’ll come back to phone number setup later. First, let’s talk about Outbound SMS. application makes a POST request to Twilio and Twilio sends a text message. It’s that simple. We’ll start in Visual Studio 2017 by creating a new ASP.NET Web Application targeting .NET Framework 4.5.2 that uses MVC. Once the project completes spinning up we’ll use the Package Manager to install the Twilio NuGet packages. Namely we’ll Install-Package Twilio to get the helper library for the REST API. And then we’ll also Install-Package Twilio.AspNet.MVC which provides some helper conveniences for our MVC application. Then we’ll create an Empty MVC5 Controller that we’ll name SmsController which is going to hold the logic for our application. Inside of the controller, we’ll bring in some dependencies. We need the System.Configuration namespace to access AppSettings. Then we’ll pull in all of the namespaces for the Twilio helper library that we just installed. We’ll change the class for the Controller to TwilioController which is going to give us a helper when we talk about inbound SMS. But first, let’s do outbound SMS. So we’ll create an Action Method called SendSms() and the first thing we need in here are a couple of credentials. We need the Account SID and Auth Token from our Twilio account. These look like this but I’ve got them stored in AppSettings so we’ll get them from there instead, pulling in both the TwilioAccountSid and the TwilioAuthToken using the ConfigurationManager. Next, we’ll initialize the Twilio REST client by passing in the accountSid and authToken that we just got out of AppSettings. Then, we can create a message but to create a message we need PhoneNumbers. The To PhoneNumber is going to be my phone number. Phone numbers look like this but you guessed it, I’ve got mine stored in AppSettings that I don’t have to show it to you! So we’ll pull that from there. Then the From number is going to be that Twilio phone number that we just purchased from the Numbers Console so we’ll paste that in there. Next we’ll create a message using MessageResource.Create. We’ll be passing in the To phone number and the From phone number as well as the text to go into the message as the body which is: “This is the ship that made the Kessel Run in 14 parsecs? [yeah that one…it’s 12!] Finally, we’ll return the message SID so that we’ll know the message was sent. We’ll run our code and head over to the browser. Once the application is running we can go to /sms/sendsms and we’ll get a text message sent to our phone. It really is that easy to do outbound SMS in C#. Now let’s talk about Inbound SMS. A user sends a text message to your Twilio phone number. Twilio makes an HTTP request to your app, and your app returns some instructions in the form of something called TwiML. If the TwiML looks like this then Twilio sends a text message back to the user’s phone. Let’s write the code to make that happen. We’ll add a new Action Method to our SmsController called ReceiveSms(). Inside of ReceiveSms we’re going to create the response to Twilio’s HTTP request using the MessagingResponse class. We’ll set the Message on our response to be “The Robots are coming! Head for the hills!” and then we’ll return the TwiML representation of that response. We’ll give our code a run. The problem is it’s running on localhost so we need to use something to create an HTTP tunnel. We’re gonna use ngrok. There’s an extension that you can find in the Visual Studio extensions that makes this really easy. You just go to Tools->Start ngrok Tunnel and it’ll fire up a tunnel to the right port. This gives us a publicly addressable URL that we can cpoy and take back over to the Twilio Phone Numbers Console. We’ll paste it in where it says “A Message Comes in” Webhook and add /sms/receivesms to the end of the URL. If I save that, I can take back out my cellphone and send a message back saying hi to our Twilio phone number and we should get that robots response. And there it is, that’s all there is to it to handling inbound SMS with C#. That’s gonna do it for this video. Thank you so much for watching. If you enjoyed this, please click or tap the Twilio logo in the bottom left to subscribe so you know when we make more. If you wanna get the code there’s a button in the bottom right for that. If you want to watch another video, I picked one just for you Until next time, I’m outta here. ♪♪♪ music ♪♪♪ ♪♪♪ music ♪♪♪

, , , , , , , , , , , ,

Post navigation

39 thoughts on “How to Send and Receive SMS Messages Using C# and ASP.NET

  1. Is it necessary to get a new phone number if I only want to send outbound (for example, sending a code so the user can input it into my web app for authentication purposes, or two-factor)?

  2. That video tutorial was amazingly good, concise and working. The only parts left out were probably the calling pages that call these functions, but that's ok.

  3. thanks for video, how to receive and store a sms in visual studio C#? Is it possible to do a more efficient data pull for new SMS messages using c# only, i need store sms in sql database

  4. I did the following steps: Install-Package Twilio and Install-Package Twilio.AspNet.Mvc they are installed correctly, but TwilioClient.Init (accountSid, authToken) is not recognized, I'm using visual studio 2015,can you help me?

  5. i successfully send the sms but not receiving the sms from my mobile phone but all configuration is working fine would you please please sir it will be great favor

  6. I live in a country which is not showing in your list where we can buy numbers. So I can not test the receive SMS option, as the numbers only allows domestic SMS. Is there a way or workaround I can test this.

  7. This is Fuking annoying to use this code. I have tried 20 times to send the SMS with the code you have shown in the video and from the documentation provided in the website. nothing works. u have any suggestion how to do or you can shut down your company if this way you gonna do the customer support. this is no point to connect to customer support. Even you can't do follow up if you are in trial account. So now what the plan?

  8. I get an error in ReceiveSms. TwiML(response); Generates an error, "TwiML can't be used as a method". Can't find anything anywhere that explains it. Just thought I would have seen this, since, My code is exactly the same. The SendSms works fine. But I can't build with this error present.

  9. Webconfig file:
    <configuration>

    <appSettings>

    <add key="TwilioAccountSid" value="PutAccountSidHere"/>

    <add key="TwilioAuthToken" value="PutValueHere"/>

    <add key="MyPhoneNumber" value="+PutNumberHere"/>

  10. Twilio.Exceptions.AuthenticationException: 'Username can not be null. This is the error I get while trying to run the sms/sendsms. Does anyone know what is the problem? How do I instantiate a null username?

  11. Do we have any chance to create live chat with mobile numbers & to integrate in our web application using twilio?

Leave a Reply

Your email address will not be published. Required fields are marked *