Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To send messages to multiple phone numbers using Twilio SMS and .NET, you can create a loop that iterates through a list of phone numbers, and then calls the Twilio API for each phone number. Here's an example code snippet:

using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;

class Program
{
    static void Main(string[] args)
    {
        TwilioClient.Init("YOUR_ACCOUNT_SID", "YOUR_AUTH_TOKEN");

        // List of phone numbers to send SMS to
        var phoneNumbers = new string[] { "+1234567890", "+1112223333", "+4445556666" };

        // Loop through phone numbers and send SMS for each
        foreach (var phoneNumber in phoneNumbers)
        {
            var message = MessageResource.Create(
                body: "Hello from Twilio!",
                from: new Twilio.Types.PhoneNumber("YOUR_TWILIO_PHONE_NUMBER"),
                to: new Twilio.Types.PhoneNumber(phoneNumber)
            );

            Console.WriteLine($"SMS sent to {phoneNumber}");
        }
    }
}

In this example, we first initialize the Twilio client with our account SID and auth token. We then create an array of phone numbers to send SMS to.

We loop through each phone number and call the MessageResource.Create method to send an SMS message with the text "Hello from Twilio!" from our Twilio phone number to the current phone number in the loop.

Finally, we output a message indicating that the SMS was sent to each phone number.

Note that you'll need to replace YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN, and YOUR_TWILIO_PHONE_NUMBER with your own account information and Twilio phone number.