Sending SMS Notifications with Azure Communication Services in .NET

Introduction

In this article, we will send SMS Notifications with Azure Communication Service using .NET. This is an alternative to one of my previous articles, "Sending SMS Notifications with Azure Communication Service using Python." Instead of Python, let's achieve this in .NET.

Step 1. Install the Azure Communication Services SDK.

Install-Package Microsoft.Azure.Communication.Sms

Step 2. Import necessary libraries.

using Azure;
using Azure.Communication;
using Azure.Communication.Sms;
using System;
using System.Threading.Tasks;

Step 3. Azure Communication Services Connection String.

string connectionString = "<YOUR_CONNECTION_STRING>";

Replace <YOUR_CONNECTION_STRING> with the actual connection string from the Azure Communication Services resource.

Step 4. SmsClient.

var smsClient = new SmsClient(connectionString);

Step 4. Initialize the phone number and message that you need to send.

var toPhoneNumber = "<TO_PHONE_NUMBER>";

var message = "Hello, this is a test message!";

Step 5. Sending SMS.

try
{
    SendSmsOptions smsOptions = new SendSmsOptions(new PhoneNumber(toPhoneNumber), message);
    Response response = await smsClient.SendAsync(smsOptions);
    Console.WriteLine($"SMS sent successfully. Message Id: {response.Value}");
}
catch (RequestFailedException ex)
{
    Console.WriteLine($"Failed to send SMS. Error: {ex.Message}");
}

By following these steps, we can initiate SMS notifications using .NET.


Similar Articles