Send SMS using Twillio in Asp.Net MVC
Installing the Twilio NuGet package
NuGet\Install-Package Twilio.AspNet.Mvc -Version 6.0.0
After installing the Twilio package, we are going to modify the web config with the account SID and the auth token found from the Twilio dashboard
<appSettings> <add key="config:AccountSid" value="AC3936d9aqq2323fedbdddd0ecd6df37199" /> <!--Replace with your AccountSid--> <add key="config:AuthToken" value="52b86f79c19bsassasasas704110dddddc26" /> <!--Replace with your AuthToken--> <add key="config:TwilioPhoneNum" value="+1845344098435" /> <!--Replace with your TwilioPhoneNum--> </appSettings>
The next part of our application is the API controller that will receive our requests. Let’s create the SmsController class within a Controllers folder:
using System; using System.Configuration; using System.Web.Mvc; using Twilio; using Twilio.Rest.Api.V2010.Account; namespace TwillioMVC.Controllers { public class SmsController : Controller { // GET: Sms public ActionResult Index() { return View(); } public ActionResult Create() { string accountSid = Convert.ToString(ConfigurationManager.AppSettings["config:AccountSid"]); string authToken = Convert.ToString(ConfigurationManager.AppSettings["config:AuthToken"]); string phone = Convert.ToString(ConfigurationManager.AppSettings["config:TwilioPhoneNum"]); TwilioClient.Init(accountSid, authToken); var message = MessageResource.Create( body: "Hello Code2night " + DateTime.Now.ToString("MM/dd/yyyy hh:mm tt"), from: new Twilio.Types.PhoneNumber(phone), to: new Twilio.Types.PhoneNumber("+919034589555") ); ViewData["Success"] = message.Sid; return View(); } } }
So, this is how we can integrate Twillio in Asp.net and send sms using Twillio in Asp.net.