.NET Quickstart: Send SMS and MMS Text Messages in C#

In this C#.NET Quickstart, we show just how easy it is to get started sending SMS and MMS Messages using Twilio's C# Helper Library and the Twilio REST API. You'll use your C# skills to send SMS and MMSes to invite multiple contacts to a Monkey Party.

Let's go bananas!

Send SMS Messages in C# and .NET

Our [twilio-csharp][twilio-csharp] helper library makes it extremely easy to add outgoing SMS capabilities to your application. We'll prove it in under 50 lines - with comments included.

Open a file called SmsSender.cs and add the following code:

Loading Code Samples...
Language
SDK Version:
  • 4.x
  • 5.x
using System;
using System.Collections.Generic;
using Twilio;

namespace Quickstart
{
    class SmsSender
    {
        static void Main(string[] args)
        {
            // Find your Account Sid and Auth Token at twilio.com/user/account
            const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            const string authToken = "your_auth_token";

            // Initialize the Twilio client
            var twilio = new TwilioRestClient(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
                twilio.SendMessage(
                    "555-555-5555",  // From number, must be an SMS-enabled Twilio number
                    person.Key,  // To number, if using Sandbox see note above
                    // Message content
                    $"Hey {person.Value} Monkey Party at 6PM. Bring Bananas!",
                );

                Console.WriteLine($"Sent message to {person.Value}");
            }
        }
    }
}
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)
        {
            // Find your Account Sid and Auth Token at twilio.com/user/account
            const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            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}");
            }
        }
    }
}
Send SMS using Twilio

The from number must be a valid Twilio phone number. The to number can be any outgoing number. Note that if you are using a Trial Account, you can only use Twilio Console Verified Caller IDs.

Send MMS Messages in C# and .NET

It's also easy to send an outgoing MMS using Twilio and C#. To send an MMS, you also make an HTTP POST request to the [Messages resource][sending-messages] 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.

Loading Code Samples...
Language
SDK Version:
  • 4.x
  • 5.x
using System;
using System.Collections.Generic;
using Twilio;

namespace Quickstart
{
    class SmsSender
    {
        static void Main(string[] args)
        {
            // Find your Account Sid and Auth Token at twilio.com/user/account
            const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            const string authToken = "your_auth_token";

            // Initialize the Twilio client
            var twilio = new TwilioRestClient(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
                twilio.SendMessage(
                    "555-555-5555",  // From number, must be an SMS-enabled Twilio number
                    person.Key,  // To number, if using Sandbox see note above
                    // Message content
                    $"Hey {person.Value} Monkey Party at 6PM. Bring Bananas!",
                    new string[] { "https://demo.twilio.com/owl.png" }
                );

                Console.WriteLine($"Sent message to {person.Value}");
            }
        }
    }
}
using System;
using System.Collections.Generic;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace Quickstart
{
    class MmsSender
    {
        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}");
            }
        }
    }
}
Send MMS using Twilio

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.

Send SMS Messages in C# and .NET via REST API

And so we don't forget - it's really easy to send an outgoing SMS sans-Helper Library using Twilio's REST API. To send an SMS, make an HTTP POST request to the Messages resource.

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


Next: Reply to Incoming Text Messages with C#.NET »

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.

1 / 1
Loading Code Samples...
SDK Version:
  • 4.x
  • 5.x
using System;
using System.Collections.Generic;
using Twilio;

namespace Quickstart
{
    class SmsSender
    {
        static void Main(string[] args)
        {
            // Find your Account Sid and Auth Token at twilio.com/user/account
            const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            const string authToken = "your_auth_token";

            // Initialize the Twilio client
            var twilio = new TwilioRestClient(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
                twilio.SendMessage(
                    "555-555-5555",  // From number, must be an SMS-enabled Twilio number
                    person.Key,  // To number, if using Sandbox see note above
                    // Message content
                    $"Hey {person.Value} Monkey Party at 6PM. Bring Bananas!",
                );

                Console.WriteLine($"Sent message to {person.Value}");
            }
        }
    }
}
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)
        {
            // Find your Account Sid and Auth Token at twilio.com/user/account
            const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            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}");
            }
        }
    }
}
SDK Version:
  • 4.x
  • 5.x
using System;
using System.Collections.Generic;
using Twilio;

namespace Quickstart
{
    class SmsSender
    {
        static void Main(string[] args)
        {
            // Find your Account Sid and Auth Token at twilio.com/user/account
            const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            const string authToken = "your_auth_token";

            // Initialize the Twilio client
            var twilio = new TwilioRestClient(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
                twilio.SendMessage(
                    "555-555-5555",  // From number, must be an SMS-enabled Twilio number
                    person.Key,  // To number, if using Sandbox see note above
                    // Message content
                    $"Hey {person.Value} Monkey Party at 6PM. Bring Bananas!",
                    new string[] { "https://demo.twilio.com/owl.png" }
                );

                Console.WriteLine($"Sent message to {person.Value}");
            }
        }
    }
}
using System;
using System.Collections.Generic;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace Quickstart
{
    class MmsSender
    {
        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}");
            }
        }
    }
}