Add SMS Controller
Right-click on the Controllers Folder and select add->Controller. Select "Add Empty MVC Controller" and name it SmsController.
Add using statements for Vonage.Messaging, Vonage.Request, and Microsoft.Extensions.Configuration at the top of this file.
Inject Configuration
Dependency inject an IConfiguration object via the constructor like so:
public IConfiguration Configuration { get; set; }
public SmsController(IConfiguration config)
{
Configuration = config;
}
Add Send SMS Action
Next, add a Send SMS Action to the controller:
[HttpPost]
public IActionResult Sms(Models.SmsModel sendSmsModel)
{
if (ModelState.IsValid)
{
try
{
var VONAGE_API_KEY = Configuration["VONAGE_API_KEY"];
var VONAGE_API_SECRET = Configuration["VONAGE_API_SECRET"];
var credentials = Credentials.FromApiKeyAndSecret(VONAGE_API_KEY, VONAGE_API_SECRET);
var client = new SmsClient(credentials);
var request = new SendSmsRequest { To = sendSmsModel.To, From = sendSmsModel.From, Text = sendSmsModel.Text };
var response = client.SendAnSms(request);
ViewBag.MessageId = response.Messages[0].MessageId;
}
catch(VonageSmsResponseException ex)
{
ViewBag.Error = ex.Message;
}
}
return View("Index");
}
How to Receive SMS Delivery Receipts with ASP.NET Core MVC
Delivery receipts allow you to get information about when an SMS is delivered to a user's handset. This tutorial shows how you can receive these delivery receipt notifications in your ASP .NET application.
Steps
1
Introduction to this tutorial2
Prerequisites3
Create the SMS Project File4
Add Vonage Dotnet SDK5
Create Send SMS Model6
Create a Send SMS View7
Set up Startup Route8
Add an SMS Controller9
Add Delivery Receipt Route to Controller10
Configure the ASP.NET App11
Run the .NET App12
Conclusion