ASP.NET MVC - Sending SMS Messages Using Nexmo API

Introduction

In this article, we will demonstrate how we can send an SMS message from Asp.Net MVC application using Nexmo SMS API. As you know, sending an SMS message is a feature which is used by all web applications in order to verify the password, notifications, etc.

So, this post is an opportunity to discover step by step how we can implement Nexmo SMS API. I hope you will like it.

Prerequisites

Make sure you have installed Visual Studio 2017 (.NET Framework 4.6.1) and SQL Server.

In this post, we are going to:

  • Create MVC application.
  • Installing Nexmo SMS API.
  • Add appsettings file.
  • Create our SMSMessage controller.
  • Create HTML page for the demo.             

Create your MVC application

Open Visual Studio and select File >> New Project.

The "New Project" window will pop up. Select ASP.NET Web Application (.NET Framework), name your project, and click OK.

MVC

Next, a new dialog will pop up for selecting the template. We are going choose MVC template and click OK.

MVC

Once our project is created, the next step is to install Nexmo SMS API.

Installing Nexmo SMS API

In solution explorer, right click on References >> Manage NuGet Packages.

Now, type Nexmo.Csharp.Client in search input and then click on Install button.

MVC

Create Nexmo Account

After installing Nexmo packages from NuGet, now, we should sign up for Nexmo account in order to get API credentials which will allow us to communicate with Nexmo API.

MVC
After creating our account successfully. We will be redirected to the following page.

MVC
Add appsettings file

Here, we need to add new JSON file which is named appsettings. Now, inside this file, we must copy-paste the API credentials which were generated in the previous picture.

MVC

Appsettings.json

  1. {  
  2.   "appSettings": {  
  3.     "Nexmo.UserAgent""NEXMOQUICKSTART/1.0",  
  4.     "Nexmo.Url.Rest""https://rest.nexmo.com",  
  5.     "Nexmo.Url.Api""https://api.nexmo.com",  
  6.     "Nexmo.api_key""11549690",  
  7.     "Nexmo.api_secret""9c9e31d1d207a5da",  
  8.     "NEXMO_FROM_NUMBER""YOUR_PHONE_NUMBER"  
  9.   }  
  10. }  

Create a controller

Now, we are going to create a controller. Right click on the controllers folder> > Add >> Controller>> selecting MVC 5 Controller – Empty >> click Add. In the next dialog, name the controller as SMSMessageController and then click Add.

MVC

MVC

SMSMessageController.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using Nexmo.Api;  
  7. using SendSMSMessages.ViewModels;  
  8.   
  9. namespace SendSMSMessages.Controllers  
  10. {  
  11.     public class SMSMessageController : Controller  
  12.     {  
  13.         // GET: SMSMessage  
  14.         public ActionResult Index()  
  15.         {  
  16.             return View();  
  17.         }  
  18.   
  19.         [HttpGet]  
  20.         public ActionResult SendMessage()  
  21.         {  
  22.             return View();  
  23.         }  
  24.   
  25.         [HttpPost]  
  26.         public ActionResult SendMessage(Message message)  
  27.         {  
  28.             var results = SMS.Send(new SMS.SMSRequest  
  29.             {  
  30.                 from = Configuration.Instance.Settings["appsettings:NEXMO_FROM_NUMBER"],  
  31.                 to = message.To,  
  32.                 text = message.ContentMsg  
  33.             });  
  34.   
  35.   
  36.             return View();  
  37.         }  
  38.     }  
  39. }  

As you can see SMSMessage controller contains SendMessage action decorated by [httpGet] attribute and is used to display SMSMessage.cshtml view then we have SendMessage decorated by [httpPost] attribute which is responsible to send an SMS message to a phone number as given in message object.

I’d like to explain that the send method accepts SMSRequest objects that contain the three main properties (from, to, text) as shown in the code snippet.

View model

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace SendSMSMessages.ViewModels  
  7. {  
  8.     public class Message  
  9.     {  
  10.         public string To { get; set; }  
  11.         public string ContentMsg { get; set; }  
  12.     }  
  13. }  

SendMessage.cshtml

  1. @model SendSMSMessages.ViewModels.Message  
  2.   
  3. @{  
  4.     ViewBag.Title = "SendMessage";  
  5. }  
  6.   
  7. <h2>Send Message</h2>  
  8.   
  9. @using (Html.BeginForm())   
  10. {  
  11.     @Html.AntiForgeryToken()  
  12.       
  13.     <div class="form-horizontal">  
  14.         <h4>Message</h4>  
  15.         <hr />  
  16.         @Html.ValidationSummary(true""new { @class = "text-danger" })  
  17.         <div class="form-group">  
  18.             @Html.LabelFor(model => model.To, htmlAttributes: new { @class = "control-label col-md-2" })  
  19.             <div class="col-md-10">  
  20.                 @Html.EditorFor(model => model.To, new { htmlAttributes = new { @class = "form-control" } })  
  21.                 @Html.ValidationMessageFor(model => model.To, ""new { @class = "text-danger" })  
  22.             </div>  
  23.         </div>  
  24.   
  25.         <div class="form-group">  
  26.             @Html.LabelFor(model => model.ContentMsg, htmlAttributes: new { @class = "control-label col-md-2" })  
  27.             <div class="col-md-10">  
  28.                 @Html.EditorFor(model => model.ContentMsg, new { htmlAttributes = new { @class = "form-control" } })  
  29.                 @Html.ValidationMessageFor(model => model.ContentMsg, ""new { @class = "text-danger" })  
  30.             </div>  
  31.         </div>  
  32.   
  33.         <div class="form-group">  
  34.             <div class="col-md-offset-2 col-md-10">  
  35.                 <input type="submit" value="Send SMS" class="btn btn-default" />  
  36.             </div>  
  37.         </div>  
  38.     </div>  
  39. }  
  40.   
  41. <div>  
  42.     @Html.ActionLink("Back to List""Index")  
  43. </div>  
  44.   
  45. @section Scripts {  
  46.     @Scripts.Render("~/bundles/jqueryval")  
  47. }  

Demo

Now, our application is ready. We can run and see the output in the browser.

MVC

MVC

That’s all. Please send your feedback and queries in the comments box. 


Similar Articles