Sending SMS with .NET

How to Send SMS Messages with ASP.NET MVC Framework

Published March 23, 2017 by Sidharth Sharma

The Nexmo SMS API lets you send and receive text messages around the world. This tutorial shows you how to use the Nexmo C# Client Library to send SMS messages from your ASP.NET MVC web app.

GitHub Nexmo Community View the source code on GitHub

Prerequisites

To get started with the Nexmo Client Library for .NET, you will need:

  • Visual Studio 2017 RC
  • Windows machine

Getting Started

Sign up for a Nexmo account and get your API credentials from the Getting Started page of the dashboard. You will be adding these credentials to your project in order to communicate with the Nexmo API. Navigate to the Numbers section of the dashboard to buy a phone number.

ASP.NET Project Setup

First, open up Visual Studio and create a new ASP.NET Web Application (.NET Framework) project.

Create New Project

Select the MVC Template and ensure the Authentication type is set to No Authentication. Click OK to create the project.

MVC Template

Next, install the Nexmo C# Client Library via the NuGet Package Manager Console.

Install Client Library

Also, add the following package to enable debug logging in the output window via the Package Manager Console:

Next, under the Tools dropdown menu, locate NuGet Package Manager and click Manage NuGet Packages for Solution. Under the Updates tab, select the Update All Packages box and click the Update button.

Update NuGet Packages

Diving Into the ASP.NET Project Code

Add a JSON file (appsettings.json) to your project. Inside this file, add your Nexmo credentials.

Create a new controller (SMSController.cs). In this controller, create an action method called Send. Above the method, add a HttpGetAttribute to allow the user to navigate to the corresponding view.

Afterwards, click on the Views folder and add a new folder called SMS. Within this folder, create a new view. (`Send.cshtml’). Then, add a form to the view with two input tags (type = “text”) for the destination number and the message to be sent. Lastly, add an input tag (type = “submit”) to submit the form.

Back in the SMSController, add the following using statement to the top of the file.

Add another action method named Send with two string parameters: to and text. Within this method, add the code below to send the text using the parameters as the to and text values. The from number is your Nexmo virtual number (retrieved from the appsettings.json).

Run the app and navigate to the correct route localhost:PORT/SMS/Send. Enter the message you wish to send and the destination number and click Send.

Send SMS Messages

SMS sent using C# Client Library

There you have it! Sending an SMS in .NET using the Nexmo C# Client Library is that simple! Stay tuned for the next part of this series on how to receive an SMS in .NET Standard!

Feel free to reach out via e-mail or Twitter if you have any questions!

Helpful Links