program to send sms using twilio in csharp

Here is an example of how to send an SMS using the Twilio API in C#:

First, you will need to install the Twilio package via NuGet:

  1. Open Visual Studio
  2. Right-click on your project in the Solution Explorer
  3. Click "Manage NuGet Packages"
  4. Search for "Twilio"
  5. Click "Install" and accept any necessary terms and conditions

Once you have installed the Twilio package, you can use the following code to send an SMS:

main.cs
// Find your Account Sid and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;

class Program
{
    static void Main(string[] args)
    {
        // Your Account Sid and Auth Token from twilio.com/console
        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "your_auth_token";
        TwilioClient.Init(accountSid, authToken);

        var message = MessageResource.Create(
            body: "This is the message body",
            from: new Twilio.Types.PhoneNumber("+14158141829"), // Twilio number
            to: new Twilio.Types.PhoneNumber("+15558675310") // Your recipient's phone number
        );

        Console.WriteLine(message.Sid);
    }
}
819 chars
25 lines

Note:

  • Replace accountSid and authToken with your own Twilio account credentials.
  • Replace from with your Twilio phone number (you may need to purchase one if you haven't already done so).
  • Replace to with your recipient's phone number (in E.164 format).

This code will send an SMS with the body "This is the message body" from your Twilio phone number to the recipient's phone number.

related categories

gistlibby LogSnag