In this article we will discuss how to integrate SMS in ASP.NET Core using Twilio free account. We integrate SMS in ASP.NET Core using Twilio free account. For this article we consider ASP.NET Core C#, at the end of this article you will have full depth idea about how we can easily integrate SMS in ASP.NET Core. Also please follow our article about Unit Testing in ASP.NET C#.

Find Source Code

What is Twilio?

Twilio’s SMS API helps you add robust messaging capabilities to your applications. Using this REST API, you can send and receive SMS messages, track the delivery of sent messages, and retrieve and modify message history.

How to Configure Twilio account.

To Configure Twilio account visit the link.

  • Register the Twilio account with the link.
  • Configure the Product according to your requirement.
  • After configuration it redirect to Twilio dashboard.
  • On the dashboard move to Twilio Console.
  • You can find here the Account SID and Auth Token that should use in further.

Get a Twilio trial phone number

The Trail number is setup now like below image and select Choose this Number.

You can generate the code as per your requirement to select the below highlighted button.

There is find different code JavaScript, PHP, Java, Python, Ruby, .NET. As we are using .NET so we generate accordingly, also the Twilio source code available in GitHub.

Creating ASP.NET Core application and Integrate Twilio SMS

  • To Create ASP.NET Core application we follow ASP.NET Core 5.0 with ASP.NET Core Razor Pages.
  • Add the below package in the project.
  • After installing the Twilio package, we should replace the Account SID and the AUTH token found on the Twilio console. with the free trial number. The source code is look like this.
 public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;

        public IndexModel(ILogger<IndexModel> logger)
        {
            _logger = logger;
        }
        public void OnGet()
        {
            SendSMS("+19152282762", "+9199*******", "Hey CoreSpider! this is a test message from Twilio test in ASP.NET Core");
            // **** is put your recepient mobile number
        }
        public void SendSMS(string from, string to, string message)
        {
            var accountSid = "ACe48e040bd2237b16627c54cc32392902"; // SID form Twilio console
            var authToken = "75f9ab9f5729dfc6bf3*********";// Auth token form Twilio console
            TwilioClient.Init(accountSid, authToken);
            try
            {
                MessageResource response = MessageResource.Create(
                    body: message,
                    from: from,
                    to: to
                );
            }
            catch (Exception ex)
            {
            }
        }
    }
  • We are done all the changes now, then run the application and see the SMS in the provided mobile number with your test message like below.

How to send SMS in Multiple number.

Using Twilio SMS you can send the SMS to multiple numbers using below steps. Replace the below code with try catch in above code.

 // make an associative array of people we know, indexed by phone number
            var people = new Dictionary<string, string>() {
                {"+14158675308", "Curious George"},
                {"+12349013030", "Boots"},
                {"+12348134522", "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}");
            }

Source Code

Please find source code in the GitHub link

Find source Code

Summary

 Hope you liked this article on Send SMS with Twilio in ASP.Net Core and C#. We would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Have you enjoyed this post and found it useful? If so, please consider supporting me:

Buy me a coffeeBuy me a coffee

Latest Posts

Running WordPress on ASP.NET Core with Peachpie
Running WordPress on ASP.NET Core with Peachpie

This article explain about Running WordPress on ASP.NET Core with Peachpie, We discussed here how without using PHP, or any Read more

Deploy ASP.NET Core Web App in Azure Virtual Machine
Deploy ASP.NET Core Web App in Azure Virtual Machine

This article explain about Deploy ASP.NET Core Web App in Azure Virtual Machine. We discuss here how to create VM Read more

Integrate Razorpay Payment Gateway in ASP.NET Core
Integrate Razorpay Payment Gateway in ASP.NET Core

This article explain about Integrate Razorpay Payment Gateway in ASP.NET Core. We discuss here how to implement Razorpay gateway in Read more

CQRS pattern with MediatR in ASP.NET Core 5.0
CQRS pattern with MediatR in ASP.NET Core 5.0

This article explain CQRS design pattern with MediatR in ASP.Net Core 5.0. CQRS is a design pattern that separated the Read more

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Pin It on Pinterest

Share This