Sending SMS with .NET

How to Send SMS Messages with ASP.NET MVC Framework

Published March 23, 2017 by Sidharth Sharma

The Vonage 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

Vonage API Account

To complete this tutorial, you will need a Vonage API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the Vonage API Dashboard.

This tutorial also uses a virtual phone number. To purchase one, go to Numbers > Buy Numbers and search for one that meets your needs. If you’ve just signed up, the initial cost of a number will be easily covered by your available credit.

Start building with Vonage

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 Vonage API 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 Vonage 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

Leave a Reply

Your email address will not be published.

Get the latest posts from Nexmo’s next-generation communications blog delivered to your inbox.

By signing up to our communications blog, you accept our privacy policy , which sets out how we use your data and the rights you have in respect of your data. You can opt out of receiving our updates by clicking the unsubscribe link in the email or by emailing us at [email protected].