How to Send SMS in .NET Using Plivo’s SMS API

Your company has settled on Plivo to handle its voice and messaging communications, and now it’s your job to start integrating Plivo into your company’s applications. Don’t worry — Plivo has an SDK to help you out. Let’s see how to send and receive messages through Plivo in a .NET Core application.

Install the Plivo SDK

$ dotnet new console
$ dotnet add package Plivo --version 4.15.1

Find your Auth ID and Auth Token

Choose a phone number

SMS regulations followed by carriers vary from country to country. For messages to countries other than the US and Canada, you might want to register an alphanumeric sender ID for your messages. You can learn more about the use of alphanumeric sender ID and register one from your Plivo Console.

Send an SMS message

using System;
using System.Collections.Generic;
using Plivo;
namespace testplivo
{
internal class Program
{
public static void Main(string[] args)
{
var api = new PlivoApi("<auth_id>","<auth_token>");
var response = api.Message.Create(
src: "+14156667777",
dst: new List<String> { "+14156667778" },
text: "Hello, this is test message"
);
Console.WriteLine(response);
}
}
}

Note: If you’re using a Plivo trial account, you can send messages only to phone numbers that have been verified with Plivo. You can verify a phone number using the Sandbox Numbers page of the Console.

Save the file and use the below command to run it.

dotnet run

Receive an SMS message

Begin by setting up a .NET Core app. Create a project directory with the command

mkdir receivesmsapp

Change to that directory and initialize the model-view-controller (MVC) architecture with the command

dotnet new mvc --no-https

Then install the Plivo .NET package with the command

dotnet add package Plivo --version 4.15.1

Change to the Controllers directory under the receivesmsapp directory. Create a Controller called ReceiveSmsController.cs and paste in this code.

using System;
using Microsoft.AspNetCore.Mvc;
namespace ReceiceSms.Controllers
{
public class ReceiveSmsController : Controller
{
// GET: /<controller>/
public String Index()
{
// Sender's phone number
String from_number = Request.Form["From"];
// Receiver's phone number
String to_number = Request.Form["To"];
// The text which was received
String text = Request.Form["Text"];
// Print the message
Console.WriteLine("Message received - From: {0}, To: {1}, Text: {2}", from_number, to_number, text);
return "Message received";
}
}
}

Before you can test the app you have to update Properties/launchSettings.json by setting the applicationUrl as

"applicationUrl": "http://localhost:5000/"

Then start the local server with the command

dotnet run

You should see your basic server app in action on http://localhost:5000/receivesms/.

That’s fine for testing, but it’s not much good if you can’t connect to the internet to receive incoming messages and handle callbacks. For that, we recommend using ngrok, which exposes local servers behind NATs and firewalls to the public internet over secure tunnels. Install it and run ngrok on the command line, specifying the port that hosts the application on which you want to receive messages:

./ngrok http [portnum]

Ngrok will display a forwarding link that you can use as a webhook to access your local server using the public network.

Now you can create an application to receive SMS messages (follow our Quickstart guide for details).

Conclusion

Haven’t tried Plivo yet? Getting started is easy and only takes 5 minutes! Sign up today.

Originally published at https://www.plivo.com on March 25, 2021.

Developer Evangelist