|

|  How to Send SMS Messages Using Twilio API and C#

How to Send SMS Messages Using Twilio API and C#

October 31, 2024

Learn how to send SMS using Twilio API and C# in this step-by-step guide. Simplify messaging with easy-to-follow instructions and code examples.

How to Send SMS Messages Using Twilio API and C#

 

Install the Twilio NuGet Package

 

  • Open your C# project in Visual Studio.
  •  

  • Navigate to Tools > NuGet Package Manager > Manage NuGet Packages for Solution....
  •  

  • Search for "Twilio" in the Browse tab.
  •  

  • Select the Twilio package from the search results and click Install.

 

Configure Twilio Client Initialization

 

  • In your code, you need to initialize the Twilio client by using your Account SID and Auth Token from the Twilio Console.

 

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

public class SmsSender
{
    private const string AccountSid = "Your_Account_SID";
    private const string AuthToken = "Your_Auth_Token";

    public SmsSender()
    {
        TwilioClient.Init(AccountSid, AuthToken);
    }
}

 

Sending an SMS Message

 

  • Use the MessageResource.Create() method to send an SMS message. You need to specify details like to/from phone numbers and the message body.

 

public void SendSms(string toPhoneNumber, string fromPhoneNumber, string messageBody)
{
    var message = MessageResource.Create(
        body: messageBody,
        from: new Twilio.Types.PhoneNumber(fromPhoneNumber),
        to: new Twilio.Types.PhoneNumber(toPhoneNumber)
    );

    Console.WriteLine($"Message Sent: {message.Sid}");
}

 

Use Environment Variables for Credentials

 

  • For security reasons, it is advisable to store your Twilio credentials in environment variables instead of hardcoding them into your code.
  •  

  • Set environment variables using your operating system's method, and then retrieve them as shown below:

 

private static readonly string AccountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
private static readonly string AuthToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");

public SmsSender()
{
    TwilioClient.Init(AccountSid, AuthToken);
}

 

Handle Errors and Exceptions

 

  • It's important to handle exceptions that might occur when sending SMS messages, such as network errors or invalid numbers.
  •  

  • Use a try-catch block around the message sending logic to capture and manage these exceptions.

 

public void SendSmsWithExceptionHandling(string toPhoneNumber, string fromPhoneNumber, string messageBody)
{
    try
    {
        var message = MessageResource.Create(
            body: messageBody,
            from: new Twilio.Types.PhoneNumber(fromPhoneNumber),
            to: new Twilio.Types.PhoneNumber(toPhoneNumber)
        );

        Console.WriteLine($"Message Sent: {message.Sid}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }
}

 

Additional Tips

 

  • Consider using the asynchronous methods provided by Twilio to improve the responsiveness of your application.
  •  

  • Regularly review your Twilio usage to make sure you are operating within your monthly limits and budget.
  •  

  • Familiarize yourself with Twilio's pricing and policies regarding message sending.

 

Pre-order Friend AI Necklace

Pre-Order Friend Dev Kit

Open-source AI wearable
Build using the power of recall

Order Now

OMI AI PLATFORM
Remember Every Moment,
Talk to AI and Get Feedback

Omi Necklace

The #1 Open Source AI necklace: Experiment with how you capture and manage conversations.

Build and test with your own Omi Dev Kit 2.

Omi App

Fully Open-Source AI wearable app: build and use reminders, meeting summaries, task suggestions and more. All in one simple app.

Github →

Join the #1 open-source AI wearable community

Build faster and better with 3900+ community members on Omi Discord

Participate in hackathons to expand the Omi platform and win prizes

Participate in hackathons to expand the Omi platform and win prizes

Get cash bounties, free Omi devices and priority access by taking part in community activities

Join our Discord → 

OMI NECKLACE + OMI APP
First & only open-source AI wearable platform

a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded

OMI NECKLACE: DEV KIT
Order your Omi Dev Kit 2 now and create your use cases

Omi Dev Kit 2

Endless customization

OMI DEV KIT 2

$69.99

Perfect for developers and tech enthusiasts who want to start creating immediately.

Has 64gb of on-board memory, a speaker, vibration and a programmable button.

Comes fully assembled, doesn't require technical skills to set up.

 

IMPORTANT: On backorder. Shipping end of November 2024.

Learn more

Omi Dev Kit 2: build at a new level

Key Specs

OMI DEV KIT

OMI DEV KIT 2

Microphone

Yes

Yes

Battery

4 days (250mAH)

2 days (250mAH)

On-board memory (works without phone)

No

Yes

Speaker

No

Yes

Programmable button

No

Yes

Estimated Delivery 

-

1 week

What people say

“Helping with MEMORY,

COMMUNICATION

with business/life partner,

capturing IDEAS, and solving for

a hearing CHALLENGE."

Nathan Sudds

“I wish I had this device

last summer

to RECORD

A CONVERSATION."

Chris Y.

“Fixed my ADHD and

helped me stay

organized."

David Nigh

OMI NECKLACE: DEV KIT
Take your brain to the next level

LATEST NEWS
Follow and be first in the know

Latest news
FOLLOW AND BE FIRST IN THE KNOW

San Francisco

team@basedhardware.com
Title

Company

About

Careers

Invest
Title

Products

Omi Dev Kit 2

Openglass

Other

App marketplace

Affiliate

Privacy

Customizations

Discord

Docs

Help