Sending SMS in Asp.net Core

Sending SMS from web application has been a common requirement for web developer, especially for increasing popularity in multi factor authentication, here we learn how to send SMS in asp.net core application.

There are two popular utilities in Asp.net core application for sending SMS, we can start using those API for free of cost, one is twilio API and other one is ASPSMS built-in class.

First look at the interface IIdentityMessageService with simple SendAsync method with one parameter IdentityMessage, which has three properties, one mobile number we are sending to and other one is message string, this is just to standardize the implementation,

How to send SMS with twilio

We can use this interface to create any SMS sending service class, (we also can use this interface for email sending service.) here we see the example with twilio service class.

You may need to install and add reference of Microsoft.AspNet.Identity namespace.

using Microsoft.AspNet.Identity;

public interface IIdentityMessageService
  {
        Task SendAsync(IdentityMessage message);
 }

To use twilio service for sending SMS, you need to register in twilio.com to get your authentication token.

[ you can read your token from configuration file appsetting.json, instead of hard coding values here in code.]

First we create a class to set basics sms attributes like sms username, password and mobile number.

public class SMSoptions
{
    public string SMSAccountIdentification { get; set; }
    public string SMSAccountPassword { get; set; }
    public string SMSAccountFrom { get; set; }
}

Now let’s implement the interface we have defined above.

using Microsoft.Extensions.Options;
using System.Threading.Tasks;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace WebTrainingRoom
{
  
    public class TwilioSMSSender: IIdentityMessageService
    {
        public SMSoptions Options { get; }

        public TwilioSMSSender(IOptions<SMSoptions> optionsAccessor)
        {
            Options = optionsAccessor.Value;
        }


        public Task SendAsync(IdentityMessage message)
        {
            // Your Account SID from twilio.com/console
            var accountSid = Options.SMSAccountIdentification;

            // Your Auth Token from twilio.com/console
            var authToken = Options.SMSAccountPassword;
            TwilioClient.Init(accountSid, authToken);

          return MessageResource.CreateAsync(
             to: new PhoneNumber(message.Destination),
             from: new PhoneNumber(Options.SMSAccountFrom),
             body: message.Body);
        }
    }
}

Register in Startup File

Now let’s register SMS service classes in Startup.cs file, note, here i have added both SMS service classes just for demo purpose, you probably need to register only one service you are consuming in your application.

public void ConfigureServices(IServiceCollection services)
{
         
        #region Email and SMS(keep either one) 
         
        services.AddSingleton<IIdentityMessageService, TwilioSMSSender>();
        services.AddSingleton<IIdentityMessageService, AspSMSSender>();
        services.Configure<SMSoptions>(Configuration);
        #endregion
         
        services.AddRazorPages();
}
Send SMS with ASPSMS

Here is the example of how we can send sms using ASPSMS.SMS class.

using Microsoft.Extensions.Options;
using System.Threading.Tasks;


namespace WebTrainingRoom
{   
    public class AspSMSSender : IIdentityMessageService
    {
        public SMSoptions Options { get; }

        public AspSMSSender(IOptions<SMSoptions> optionsAccessor)
        {
            Options = optionsAccessor.Value;
        }

        public  Task SendAsync(IdentityMessage message)
        {
         
            ASPSMS.SMS smsSender = new ASPSMS.SMS();
            smsSender.Userkey = Options.SMSAccountIdentification;
            smsSender.Password = Options.SMSAccountPassword;
            smsSender.Originator = Options.SMSAccountFrom;

            smsSender.AddRecipient(message.Destination);
            smsSender.MessageData = message.Body;

            smsSender.SendTextSMS();
            return Task.FromResult(0);
        }
    }
}

Now we see how to call those class to send SMS from asp.net code.

Asp.Net Core C# Examples | Join Asp.Net MVC Course