.NET Quickstart: Sending SMS and MMS Messages via API

It's really easy to send an outgoing SMS using Twilio. To send an SMS, make an HTTP POST request to the Messages resource.

POST https://api.twilio.com/2010-04-01/Accounts/AC123456abc/Messages

Our twilio-csharp helper library makes this extremely easy. Open a file called SmsSender.cs and add the following lines:

SmsSender.cs
using System;
using System.Collections.Generic;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace Quickstart
{
    class SmsSender
    {
        static void Main(string[] args)
        {
            // Set our Account SID and Auth Token
            const string accountSid = "ACcace8ef80331044824b975feabebc5a7";
            const string authToken = "your_auth_token";

            // Initialize the Twilio client
            TwilioClient.Init(accountSid, authToken);

            // make an associative array of people we know, indexed by phone number
            var people = new Dictionary<string, string>() {
                {"+14158675309", "Curious George"},
                {"+14158675310", "Boots"},
                {"+14158675311", "Virgil"}
            };

            // Iterate over all our friends
            foreach (var person in people)
            {
                // Send a new outgoing SMS by POSTing to the Messages resource
                MessageResource.Create(
                    from: new PhoneNumber("555-555-5555"), // From number, must be an SMS-enabled Twilio number
                    to: new PhoneNumber(person.Key), // To number, if using Sandbox see note above
                    // Message content
                    body: $"Hey {person.Value} Monkey Party at 6PM. Bring Bananas!");

                Console.WriteLine($"Sent message to {person.Value}");
            }
        }
    }
}

The from number must be a valid Twilio phone number. The to number can be any outgoing number.

It's also easy to send an outgoing MMS using Twilio. To send an MMS, you also make an HTTP POST request to the Messages resource but this time specify one or more MediaUrls parameters.

MMS messages can only be sent and received by numbers having MMS capability. You can check the capabilities of numbers in the account portal or query the Available Phone Numbers resource to search for Twilio numbers that are MMS enabled.
MmsSender.cs
using System;
using System.Collections.Generic;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace Quickstart
{
    class SmsSender
    {
        static void Main(string[] args)
        {
            // Set our Account SID and Auth Token
            const string accountSid = "ACcace8ef80331044824b975feabebc5a7";
            const string authToken = "your_auth_token";

            // Initialize the Twilio client
            TwilioClient.Init(accountSid, authToken);

            // make an associative array of people we know, indexed by phone number
            var people = new Dictionary<string, string>() {
                {"+14158675309", "Curious George"},
                {"+14158675310", "Boots"},
                {"+14158675311", "Virgil"}
            };

            // Iterate over all our friends
            foreach (var person in people)
            {
                // Send a new outgoing SMS by POSTing to the Messages resource
                MessageResource.Create(
                    from: new PhoneNumber("555-555-5555"), // From number, must be an SMS-enabled Twilio number
                    to: new PhoneNumber(person.Key), // To number, if using Sandbox see note above
                    mediaUrl: new List<Uri> { new Uri("https://demo.twilio.com/owl.png") });

                Console.WriteLine($"Sent message to {person.Value}");
            }
        }
    }
}

Note that the body parameter is optional if you are sending one or more MediaUrls.

For more information about sending SMS and MMS messages, see the REST documentation.


Next: Reply to Incoming SMS »

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.