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 my previous article about Unit Testing in ASP.NET C#.
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}"); }
Conclusion
Leave behind your valuable queries and suggestions in the comment section below. Also, if you think this article helps you, do not forget to share this with your developer community. Happy Coding 🙂
Related Articles
- How to Integrate AdminLTE with ASP.NET Core
- How to run ASP.Net Core Web Application in Docker Container
- ChatGPT Integration in ASP.Net Core using OpenAI
- How to use cookies in ASP.Net Core- Complete Guide
- Deploy ASP.Net Core apps to Azure App Service
Jayant Tripathy
Coder, Blogger, YouTuberA passionate developer keep focus on learning and working on new technology.