Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to send SMS with Twilio in ASP.NET Core 5 and C#

In this blog post, you will learn how to send an SMS with Twilio in ASP.Net Core 5 or 3.1 using C#. So we will use Twilio as our SMS service to send an SMS message and automate a response when an SMS message is received.

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.

  • You may be interested in WhatsApp Bulk Message Sender Source Code

How to Send an SMS with ASP.NET Core

Let’s get started with setting up a Twilio SMS account. When we create a Twilio account, we are going to be given a free trial account with a 15.50 USD. We can use the trial balance toward purchasing a phone number and sending and receiving messages.

Now note down the Account SID and Auth Token from here. Next, we will get a Twilio number for our project by clicking on “Get a Trail Number” as shown below:-

Creating and Configuring an ASP.Net Core Project with Twilio

Let’s create a new ASP.Net Core MVC project as follows:-

Next, we will install the Twilio NuGet package in our project.

Install-Package Twilio -Version 5.53.1

After installing the Twilio package, we will replace the Account SID and the auth token found on the Twilio console.

HomeController.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Send_SMS.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Twilio;
using Twilio.Rest.Api.V2010.Account;

namespace Send_SMS.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger _logger;
        public HomeController(ILogger logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            SendSMS("+17067863614", "+918800______", "Hey there! this is a test message from Twilio.");
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }


        public void SendSMS(string from, string to, string message)
        {
            var accountSid = "AC6ba329051dea86d71c043ed03a9c46";
            var authToken = "0623f2cb43a71c0908ce8591c41208";

            TwilioClient.Init(accountSid, authToken);

            try
            {
                MessageResource response = MessageResource.Create(
                    body: message,
                    from: from,
                    to: to
                );
            }
            catch (Exception ex)
            {
            }
        }
    }
}

Note: If you’re using a trial account, you’ll notice that any messages you send will always begin with “Sent from a Twilio trial account.” Once you upgrade your account, you will no longer see this message. See in the following screenshot:-

Download Source Code

Conclusion

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

The post How to send SMS with Twilio in ASP.NET Core 5 and C# appeared first on DotNetTec.



This post first appeared on Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps API Developer, please read the originial post: here

Share the post

How to send SMS with Twilio in ASP.NET Core 5 and C#

×

Subscribe to Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps Api Developer

Get updates delivered right to your inbox!

Thank you for your subscription

×