Send SMS Using Twilio SMS API In .Net Core

Introduction:

In this post, we’ll describe how to send SMS messages using the Twilio SMS API and ASP.NET Core. For sending SMS messages across the international network, Twilio offers third-party SMS API. So this article can teach us a little bit.

Before you can send messages, you’ll need to sign up for a Twilio account and purchase a Twilio phone number.

Install-Package Twilio :

All of the SMS, video, and other associated classes, methods, and Twilio APIs will be installed by this package.

If you don’t have any twilio account then you should register a free account.

After completing registration, we can access our Twilio account’s Dashboard and Console. For sending SMS, we will obtain “Account SID & Auth Token Id” from the Dashboard.

Sending messages requires a Twilio phone number with SMS capabilities. If you don’t currently own a Twilio phone number with SMS capabilities, you’ll need to buy one. And If you register free account then you need to activate your number.

Verify PhoneNumber for Trial Account

After that you can add your number it will send OTP for verification.

Or you can buy number for sending SMS to multiple users.

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
namespace Common.WebApi.Controllers
{
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class UsersController : ControllerBase
{
        private readonly ApplicationDbContext _applicationDbContext;
        private readonly IExceptionLogger _exceptionLogger;

        
        [HttpPost]                                                                                                                                                       public async Task<IActionResult> Post(NotificationModel notificationSMS)
        {
            string accountSid = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
            string authToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX';
            string PhoneNumber = '+342654654646';

            TwilioClient.Init(accountSid, authToken);
            try
            {
                var numbersToMessage = new List<string>
                {
                   "+15558675310",
                   "+14158141829",
                   "+15017122661"
                };
                var SMS = notificationSMS.Title + System.Environment.NewLine + System.Environment.NewLine + notificationSMS.Body;

                if (numbersToMessage.Count > 0)
                {
                    foreach (var number in numbersToMessage)
                    {
                        var message = MessageResource.CreateAsync(
                            body: SMS,
                            from: new Twilio.Types.PhoneNumber(PhoneNumber),
                            to: new Twilio.Types.PhoneNumber(number.Number)
                      );
                    }
                    return Ok(true);
                }
               return Ok(false);;
              
            }
            catch (Exception e)
            {
                ExceptionModel exceptionModel = new ExceptionModel { ExceptionString = e.ToString(), PageName = "SMSSevice" };
                await _exceptionLogger.AddException(exceptionModel);
                return false;
            }
            return Ok(true);
        }
    }
}

This will create a new Message Instance for each phone number in the list.

You can send as many messages as you’d like as quickly as you can and Twilio will queue them up for delivery at your rate limit.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories