Menu

Expand
Rate this page:

Programmable SMS Quickstart for C# with .NET Framework

Looking for .NET Core? We have a quickstart for that too!

With just a few lines of code, your .NET Framework application can send and receive text messages with Twilio Programmable SMS.

This C# SMS Quickstart will teach you how to do this using our Communications REST API and the Twilio helper library for .NET.

In this Quickstart, you will learn how to:

  1. Sign up for Twilio and get your first SMS-enabled Twilio phone number
  2. Set up your development environment to send and receive messages
  3. Send your first SMS
  4. Receive inbound text messages
  5. Reply to incoming messages with an SMS

Prefer to get started by watching a video? Check out our C# SMS Quickstart video on Youtube.

Show me how it's done!

Already have a Twilio account? Go ahead and skip this section.

You can sign up for a free Twilio trial account here.

  • When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to send test messages to your phone from your Twilio account while in trial mode.
  • Once you verify your number, you'll be asked a series of questions to customize your experience.
  • Once you finish the onboarding flow, you'll arrive at your project dashboard in the Twilio Console. This is where you'll be able to access your Account SID, authentication token, find a Twilio phone number, and more.
I've got an account! What's next?

Install the Twilio CLI

We'll need to use the Twilio CLI (command line interface) for a few tasks, so let's install that next.

One of the easiest ways to install the CLI on Mac OS X is to use Homebrew. If you don't already have it installed, visit the Homebrew site for installation instructions and then return here.

Once Homebrew is installed, simply run the following command to install the CLI:

brew tap twilio/brew && brew install twilio

Updating

If you already installed the CLI with brew and want to upgrade to the latest version, run:

brew upgrade twilio

Warning for Node.js developers

If you have installed Node.js version 10.12 or higher on your Mac, you can avoid potential Node.js version conflicts by installing the CLI using npm:

npm install twilio-cli -g

Before we can install, we need to make sure you have Node.js installed (version 10.12 or above). To see if you have node installed, try running this command:

node -v

If your system reports v10.12.0 or above, you can skip the next step.

Installing Node.js on Windows

Using the Windows Installer (.msi) is the recommended way to install Node.js on Windows. You can download the installer from the Node.js download page.

Installing Twilio CLI

The CLI is installed with npm (Node Package Manager), which comes with Node.js. To install the CLI run the following command:

npm install twilio-cli -g

Note the -g option is what installs the command globally so you can run it from anywhere in your system.

Updating

If you already installed the CLI with npm and want to upgrade to the latest version, run:

npm install twilio-cli@latest -g

Before we can install, we need to make sure you have Node.js installed (version 10.12 or above). Even if you already installed Node yourself, the CLI works best when you install it using nvm. Here's how to get nvm installed on most Linux systems:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

Please visit the nvm installation instructions for additional options and troubleshooting steps. Once you have nvm installed, run the following to install and use the most recent LTS release of Node.js:

nvm install --lts
nvm use <insert version reported from above>

Installing other Twilio CLI prerequisites for Linux

Depending on your distribution, you will need to run one of the following commands:

  • Debian/Ubuntu: sudo apt-get install libsecret-1-dev
  • Red Hat-based: sudo yum install libsecret-devel
  • Arch Linux: sudo pacman -S libsecret

Installing Twilio CLI

The CLI is installed with npm (Node Package Manager), which comes with Node.js. To install the CLI run the following command:

npm install twilio-cli -g

Note the -g option is what installs the command globally so you can run it from anywhere in your system.

Updating

If you already installed the CLI with npm and want to upgrade to the latest version, run:

npm install twilio-cli@latest -g

Run twilio login to get the Twilio CLI connected to your account. Visit https://www.twilio.com/console, and you’ll find your unique Account SID and Auth Token to provide to the CLI.

You can reveal your auth token by clicking on the eyeball icon:

Reveal Your Auth Token

Now to get a phone number...

Get a phone number

If you don't currently own a Twilio phone number with SMS functionality, you'll need to purchase one. With the CLI, run:

twilio phone-numbers:buy:local --country-code US --sms-enabled

Replace US with your ISO-3166-1 country code if you would like a phone number in another country. If you aren't finding any SMS enabled numbers, try looking for a mobile number instead of a local number: twilio phone-numbers:buy:mobile --country-code DE --sms-enabled

Select a phone number to add it to your account.

Next, we need to install Visual Studio and the Twilio C# Helper Library.

No problem! Take me through the setup.

Create a new project and add the Twilio NuGet package

If you have Visual Studio installed, you are ready to get going. Otherwise, you should download and install it now.

Once you have Visual Studio, open it up and create a new Console project. Click the "Create a new project" button:

CS2019-Create-a-new-project.png

And then choose "Console App (.NET Framework)" from the list of project templates:

VS2019-New-Console-Framework.png

Once you have Visual Studio, open it up and create a new Console project. Select the "File" menu and choose "New" then "Project..." and select "Console App (.NET Framework)".

Visual Studio - New .NET Framework Project

Then, select "Tools," "NuGet Package Manager," and "Package Manager Console" from the main menu in Visual Studio and type this command:

Install-Package Twilio
All set! Let's send a text message.

Send an Outbound SMS Message with C#

Now that we have .NET and the Twilio .NET library installed, we can send an outbound text message from the Twilio phone number we just purchased with a single API request. Open the file in your new project called Program.cs and type or paste in this code sample, replacing the template code that's already there.

        
        
        
        
        This code creates a new instance of the Message resource and sends an HTTP POST to the Messages resource URI.

        Send an SMS Using Twilio with C#

        This code creates a new instance of the Message resource and sends an HTTP POST to the Messages resource URI.

        You’ll need to edit this file a little more before your message will send:

        Replace the placeholder credential values

        Swap the placeholder values for accountSid and authToken with your personal Twilio credentials. Go to https://www.twilio.com/console and log in. On this page, you’ll find your unique Account SID and Auth Token, which you’ll need any time you send messages through the Twilio Client like this. You can reveal your auth token by clicking on the 'view' link:

        Reveal your Auth Token in the Twilio Console

        Edit Program.cs and replace the values for accountSid and authToken with your unique values.

        Please note: it's okay to hardcode your credentials when getting started, but you should use configuration to keep them secret before deploying to production. ASP.NET applications should use the built-in configuration system for ASP.NET on the .NET Framework. Other types of .NET applications could use environment variables.

        Replace the "from" phone number

        Remember that SMS-enabled phone number you bought just a few minutes ago? Go ahead and replace the existing from number with that one, making sure to use E.164 formatting:

        [+][country code][phone number including area code]

        Replace the "to" phone number

        Replace the to phone number with your mobile phone number. This can be any phone number that can receive text messages, but it’s a good idea to test with your own phone, so you can see the magic happen! As above, you should use E.164 formatting for this value.

        If you are on a Twilio Trial account, your outgoing SMS messages are limited to phone numbers that you have verified with Twilio. Phone numbers can be verified via your Twilio Console's Verified Caller IDs.

        Save your changes and run your project in Visual Studio.

        That's it! In a few moments, you should receive an SMS from your Twilio number on your phone.

        Are your customers in the U.S. or Canada? You can also send them MMS messages by adding just one line of code. Check out this sending MMS tutorial to see how it's done.

        I sent the message! How do I receive them?

        Receive and Reply to Inbound SMS Messages with ASP.NET MVC

        When your Twilio number receives an incoming message, Twilio will send an HTTP request to a server you control. This callback mechanism is known as a webhook. When Twilio sends your application a request, it expects a response in the TwiML XML format telling it how to respond to the message. Let's see how we would build this in C# using ASP.NET MVC for .NET Framework.

        Create a New ASP.NET MVC Project in Visual Studio

        In Visual Studio, click "Create a new project":

        CS2019-Create-a-new-project.png

        Select "ASP.NET Web Application (.NET Framework)" from the list of templates:

        VS2019-New-ASPNET-Framework.png

        Visual Studio 2019 0 Create a new ASP.NET Web Application

        When prompted, choose "MVC" as the project type:

        In Visual Studio, select the "File" menu and choose "New" then "Project..." and select "ASP.NET Web Application (.NET Framework)."

        Visual Studio - New ASP.NET MVC Project

        Next, choose the "MVC" template.

        Visual Studio New ASP.NET Web Application (.NET Framework) - MVC

        Install the Twilio.AspNet.Mvc Package

        Select "Tools," "NuGet Package Manager," and "Package Manager Console" from the main menu in Visual Studio and type the following command:

        Install-Package Twilio.AspNet.Mvc -DependencyVersion HighestMinor
        

        Create a New Controller

        In the directory named Controllers, create a new Controller called SmsController.cs. (Right-click on the Controllers folder, select "Add >", "Controller..." and choose the "MVC 5 Controller - Empty" template.)

        Use the following code to create an action that can handle incoming messages. Be sure to use the entire code sample (note, for example, how the SmsController inherits from TwilioController).

              
              
              
              
              When your phone number receives an incoming message, Twilio will send an HTTP request to your server. This code shows how your server should respond to reply with a text message (using TwiML).

              Respond to an incoming text message

              When your phone number receives an incoming message, Twilio will send an HTTP request to your server. This code shows how your server should respond to reply with a text message (using TwiML).

              From Visual Studio, run the application (click the green "play" arrow). This should open a web browser with the ASP.NET welcome page showing with a URL something like https://localhost:44360. NOTE: your port number may be something other than 44360. You will need this URL in the next step to wire up your application to your Twilio phone number.

              I'm getting excited now - let's connect our code to Twilio!

              Configure your Webhook URL

              Now, you need to configure your Twilio phone number to call your webhook URL whenever a new message comes in. Just run this CLI command, replacing the phone number with your own Twilio phone number and ensuring the URL has your correct port number:

              twilio phone-numbers:update "+15017122661" --sms-url="https://localhost:44360/sms"
              

              The CLI will start an ngrok tunnel (so Twilio can reach your development machine) and wait patiently for incoming text messages!

              Test your application with a text

              Make sure your project is running and your ngrok tunnel is running.

              With both of those servers running, we’re ready for the fun part - testing our new ASP.NET SMS application!

              Send an SMS from your mobile phone to your Twilio phone number that's configured with this webhook. Your ASP.NET app will process the text message, and you’ll get your response back as an SMS.

              It worked! All done - what's next?

              Where to next?

              Now that you know the basics of sending and receiving SMS and MMS text messages with C# and .NET, you might want to check out these resources.

              Happy hacking!

              Rate this page:

              Need some help?

              We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

                    
                    
                    

                    Thank you for your feedback!

                    We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

                    Sending your feedback...
                    🎉 Thank you for your feedback!
                    Something went wrong. Please try again.

                    Thanks for your feedback!

                    Refer us and get $10 in 3 simple steps!

                    Step 1

                    Get link

                    Get a free personal referral link here

                    Step 2

                    Give $10

                    Your user signs up and upgrade using link

                    Step 3

                    Get $10

                    1,250 free SMSes
                    OR 1,000 free voice mins
                    OR 12,000 chats
                    OR more