Platform
Docs
Solutions
ContactLog In

3 Ways To Send Text Messages (SMS) with C#

Mobile application development is a modern process used for intelligent mobile phones. With the increased adoption of the internet and technology, most people have smart devices and are now adapting to their daily tasks from mobile applications. So, SMS has become one of the essential features of mobile devices. Almost all mobile applications use SMS features in many cases, such as validating the phone number, resetting passwords, and using two-factor authentication.

C# is one of the popular technologies among developers. As a result, there are multiple SMS APIs to use the SMS notification feature during developing applications using C#.

This article will discuss three ways to Send text messages (SMS) with the C#.

Using Twilio API

Twilio API is one of the most popular APIs used to send SMS. You can use Twilio API to send SMS for your mobile application easily. It's a programmable method you can integrate with your application.

Pros of using Twilio API

  • Support for multiple programming languages.
  • Good documentation.
  • Easy to use for development.

Cons of using Twilio API

  • It's a programmable platform and very hard to adapt to without a programming background.
  • Expensive compared to other APIs.

Tutorial: How to Send SMS Using Twilio API & C#

Here, We are using the console application using the C# to demonstrate how to use Twilio API to send SMS. You can create a free trial account using the below link.

1 https://www.twilio.com/try-twilio

You need to install the Twilio NuGet package for your C# application.

  • Install the Twilio 5.81.0 version.

Install Twilio C# LibraryYou need to configure and get the virtual phone number to send SMS using Twilio. Then you can use the Twilio virtual number to send an SMS to your phone number. Also, you can get your Twilio account SID and Twilio auth token from the console dashboard like the below figure.

Twilio Account Setup for Sending Text Messages using C#

Now you can open the program.cs file using visual studio 2022. Then use the Twilio NuGet package to create the Twilio client, and then use Create method of MessageResource to create and pass the SMS to the Twilio service to send the SMS.

1 2 3 4 5 6 7 8 9 10 11 12 13 using Twilio; using Twilio.Rest.Api.V2010.Account; string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID"); string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN"); TwilioClient.Init(accountSid, authToken); var message = MessageResource.Create( body: "Hi Yohan, We are from Twilio", from: new Twilio.Types.PhoneNumber("+13609974067"), // virtual Twilio number to: new Twilio.Types.PhoneNumber("Add your number") );

Before running the application, you must enable geo permission for your number if you use the trial version. In that case, you can enable your geo permission using this link.

1 https://console.twilio.com/us1/develop/sms/settings/geo-permissions

Note - You must also verify your number if you are using the trial account. You can verify your number using the below link.

1 https://twilio.com/user/account/phone-numbers/verified

You can successfully send SMS using the Twilio in C# application as we did here.

Using the MessageBird API

Messagebird is a cloud communications platform that enables the sending and receiving SMS text messages using web service APIs. MessageBird offers several SMS delivery methods. You may send SMS messages using email or an API connection.

Pros of using MessageBird API

  • Cost-effective.
  • User-friendly UI.
  • Good Documentation.

Cons of using MessageBird API

  • There are some deliverability problems with some mobile numbers.

Tutorial: How to Send SMS Using MessageBird API & C#

Here, We are using the console application using the C# to demonstrate how to use MessageBird API to send SMS. You can create a free trial account using the below link.

1 messagebird.com/en/signup

After creating the account, You can get a test access key from the dashboard like the below figure.

MessageBird Get Started for Sending Text Messages Then, You can install the MessageBird NuGet package for the console application.

  • Install MessageBird 4.0.0 version

MessageBird Install C# Package

Now you can open the program.cs file using visual studio 2022. Then use the MessageBird NuGet package to use the Client class and then use CreateDefault method of Client to create the client. Then use the SendMessage method of the Client class to send the SMS. There are three method parameters in the SendMessage method. They are the originators (the sender), the SMS body, and the receivers.

1 2 3 4 5 6 7 8 using MessageBird; const string YourAccessKey = "Your Acccess Key"; // your access key Client client = Client.CreateDefault(YourAccessKey); const long Msisdn = +94701397674; // your phone number MessageBird.Objects.Message message = client.SendMessage("MessageBird", "Hi! This is your first message", new[] { Msisdn });

Now you can get the response. If you get any error, You will get the error with the error code. They also have good documentation for error codes and handling errors. Then you can refer to it according to the error.

1 https://developers.messagebird.com/api/sms-messaging/#sms-error-codes

Using Courier Multi-Channel Notification API

Multi-channel notification API is a service that sends notifications through many channels like email, messages, WhatsApp, Slack, etc. For example, the Courier provides a multi-channel notification API using the uniform API to send information to all media users.

Pros of using a Multi-Channel Notification API

  • You can use multiple channels to send SMS.
  • You can use very small code-block and even use multiple channels.
  • Non-technical users also can manage SMS services.

Cons of using a Multi-Channel Notification API

  • Cost can be increased when using many channels.

Tutorial: How to Send SMS Using Courier API & C#

Here we are using Courier multi-channel notification API with C# console application for this demonstration. Courier supports popular SMS providers like Twilio, Pivilio, MessageBird, etc. We are using the MessageBird service with the Courier.

Here you can create a Courier account using the below link.

1 https://app.courier.com/register

After login into the account, you need to create the channel using one of the SMS providers. We'll use MessageBird for this example.

MessageBird Account SetupHere, you have to add originating Number and Access Key and then click the Install Provider button.

After creating the channel, route to the designer tab to create a notification. Here, you need to click the Create Notification button like the below figure.

C# Create New SMS Notification

Here, You should have to create a notification. Then, you must select SMS as the channel and MessageBird as the service provider to send via Courier.

C# SMS Channel Selection

Then, click the Publish Changes button and move to the Preview tab. Here, click the Create Test Event button to create a new event.

C# Send SMS Test Event

Using Courier Multi-Channel Notification API with C#

After creating the event, move to send tab, and then you can find the API used to send SMS and its examples. You can also find the Auth Token to access APIs.

Here, we will access Courier APIs using the HttpClient in C#. First, create the data object you need to send for the HttpPost request. Here you need to convert the object into a JSON object using the JsonSerializer.

1 2 3 4 5 6 7 8 9 10 11 var data = new { message = new { to = new { phone_number = "5558675309" }, template = "MDW2NHJ29RMTFJJ93R8H8X7Z0Y0V", data = new { recipientName = "Test" } } }; var jsonString = JsonSerializer.Serialize(data); var dataContent = new StringContent(jsonString, Encoding.UTF8, "application/json");

Then, create the instance for HttpClient to access the Courier send API.

1 2 3 4 5 6 7 8 var url = "https://api.courier.com/send"; var jsonString = JsonSerializer.Serialize(data); var dataContent = new StringContent(jsonString, Encoding.UTF8, "application/json"); var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "pk_prod_GJQ9BZ2K954N5BH4K70NMTJKGFKX"); var response = await httpClient.PostAsync(url, dataContent);

After that, You need to get the response. Here, you need to deserialize the JSON string to the response object. In that case, you need to create the response class.

1 2 3 4 5 public class ResponseDto { [JsonPropertyName("requestId")] public string RequestId { get; set; } }

Then, you need to deserialize the JSON string using the ResponseDto class.

1 2 var resultString = response.Content.ReadAsStringAsync().Result; var responseDTO = JsonSerializer.Deserialize<ResponseDto>(resultString);

We can send SMS using the Courier Multi-channel notification API very quickly. Here is the complete code block used for C# in this example.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 using System.Net.Http.Headers; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; var data = new { message = new { to = new { phone_number = "5558675309" }, template = "MDW2NHJ29RMTFJJ93R8H8X7Z0Y0V", data = new { recipientName = "Test" } } }; var url = "https://api.courier.com/send"; var jsonString = JsonSerializer.Serialize(data); var dataContent = new StringContent(jsonString, Encoding.UTF8, "application/json"); var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "pk_prod_GJQ9BZ2K954N5BH4K70NMTJKGFKX"); var response = await httpClient.PostAsync(url, dataContent); var resultString = response.Content.ReadAsStringAsync().Result; var responseDTO = JsonSerializer.Deserialize<ResponseDto>(resultString);

Conclusion

This article demonstrated three SMS APIs used in C#, the pros and cons of the APIs, and how to use them with your C# applications. I hope the information in this article made it easier for you to decide on the best service for your application.

Thank you for reading.

View More Guides

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Platform

Users

Content

Channels

Sending

Workflows

Preferences

Inbox

Workspaces

Observability

API Status

Changelog

© 2024 Courier. All rights reserved.