In this article, I am going to demonstrate you step by step, how you can send SMS from your ASP.NET MVC web application using Twilio API.

Step 1: Before we begin, you have to create an account on Twilio using this link https://www.twilio.com/try-twilio, if you already have an account you can use the login link https://www.twilio.com/login

After creating a free trial account from the above Twilio links, you would need to get your SID and AuthToken from the console as shown below

twilio-account-details-send-sms-using-mvc-min.png

Save the above details, we will use it later.

Step 2: Now, let's create a new ASP.NET MVC project in Visual Studio, by navigating to File -> New -> Project (Select 'Web' from left pane &  Select 'ASP.NET web application' from right pane)

Enter the name of your project(SendSMSUsingTwilioAPI) and click "Ok"

Now select MVC template to generate default layout.

Step 3: In this step, we will install Twilio API in our project, so navigate to Tools-> NuGet Package Manager-> Select "Manage Nuget package for this solution...".

Select the "Browse" tab and search for Twilio

twilio-sms-using-asp-net-mvc-min

After selecting the Twilio, select the projects in which you want to install the API, and click "Install".

Step 4: Now, we have done, all the basic steps, let's create a form to submit name and number to the Twilio API to send SMS, navigate to HomeController, create a new ActionMethod and name it,SendSMS

 public ActionResult SendSMS()
        {
            return View();
        }

Now, Before we create a view, let's create ViewModel for Sending SMS, right click on the Models folder for your Project, Select "Add", Select "class", then name(SMSForm.cs) it and click Ok

Create two fields for testing purpose, Name, and Number in your Model.

using System.ComponentModel.DataAnnotations;

namespace SendSMSUsingTwilioAPI.Models
{
    public class SMSForm
    {
        [Required]
        public string Name { get; set; }
        [Required]
        public string Number { get; set; }
    }
}

Now, get back to HomeController, right-click inside SendSMS ActionMethod and select "Add View", create the Form Inside View to send SMS 

@model SendSMSUsingTwilioAPI.Models.SMSForm
@{
    ViewBag.Title = "Send SMS";
}

<h2>Send SMS</h2>

@ViewBag.Message

@using (Html.BeginForm("SendSMS", "Home", FormMethod.Post))
{
    @Html.ValidationSummary()
    <div class="row">
        <div class="col-lg-2">
            Name
        </div>
        <div class="col-lg-10">
            @Html.TextBoxFor(a => a.Name)
        </div>
    </div>
    <br />
    <div class="row">
        <div class="col-lg-2">
            Number
        </div>
        <div class="col-lg-10">
            @Html.TextBoxFor(a => a.Number)
        </div>
    </div>
    <br />
    <div class="row">

        <div class="col-lg-offset-2">
            <input type="submit" class="btn btn-primary" value="Send Trial SMS" />
        </div>
    </div>
}

After entering details, we need to submit these details to C# controller and use Twilio API to send SMS, so create the Post ActionMethod

[HttpPost]
        public ActionResult SendSMS(SMSForm sms)
        {
            if (ModelState.IsValid)
            {
                // Find your Account Sid and Auth Token at twilio.com/console
                const string accountSid = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
                const string authToken = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

                TwilioClient.Init(accountSid, authToken);

                //https://support.twilio.com/hc/en-us/articles/223183068-Twilio-international-phone-number-availability-and-their-capabilities
                var to = new PhoneNumber("+91" + sms.Number);

                var message = MessageResource.Create(
                    to,
                    //First navigate to your test credentials https://www.twilio.com/user/account/developer-tools/test-credentials
                    // then you need to get Twilio number from https://www.twilio.com/console/voice/numbers
                    from: new PhoneNumber("+16XXXXXXXXX"), //  From number, must be an SMS-enabled Twilio number ( This will send sms from ur "To" numbers ). 
                    body: $"Hello " + sms.Name + " !! Welcome to Twilio SMS API example in ASP.NET MVC!!");


                ViewBag.Message = "Message Sent";
                return RedirectToAction("SendSMS");
            }
            else
            {
                return View();
            }
        }

Note: You will need a FROM number using Twilio API, so navigate to https://www.twilio.com/user/account/developer-tools/test-credentials and then select Numbers (https://www.twilio.com/console/voice/numbers), add new Trail number for your testing credentials

twilio-mobie-sms-using-asp-net-mvc-min

Final steps, Build your project and Navigate to http://localhost:54501/Home/SendSMS

send-sms-using-twilio-api-in-mvc-trial-min.png

We have added the Data Annotations in our models so without entering details and clicking "Send Trial SMS" will show validation error messages.

Enter the proper details, and Click on "Send Trial SMS", you will get the screen as below

Mesage-send-using-twilio-C-sharp-min.png

Your SMS is sent on the number entered by you in your Form, here is the screenshot of the message delivered on my mobile.

Image-1-min.jpg

That's it, we are done, feel free to ask questions related to this article.