This client library enables working with the Microsoft Azure Communication Sms service. For this release, see notes - https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/communication/Azure.Communication.Sms/README.md and https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/communication/Azure.Communication.Sms/CHANGELOG.md. Microsoft Azure Communication Sms quickstart - https://docs.microsoft.com/azure/communication-services/quickstarts/telephony-sms/send?pivots=programming-language-csharp
$ dotnet add package Azure.Communication.SmsThis package contains a C# SDK for Azure Communication Services for SMS and Telephony.
Source code | Package (NuGet) | Product documentation
Install the Azure Communication SMS client library for .NET with NuGet:
dotnet add package Azure.Communication.Sms
You need an Azure subscription and a Communication Service Resource to use this package.
To create a new Communication Service, you can use the Azure Portal, the Azure PowerShell, or the .NET management client library.
SmsClient provides the functionality to send messages between phone numbers.
using System;
using Azure.Communication.Sms;
SMS clients can be authenticated using the connection string acquired from an Azure Communication Resource in the Azure Portal.
var connectionString = "<connection_string>"; // Find your Communication Services resource in the Azure portal
SmsClient client = new SmsClient(connectionString);
Alternatively, SMS clients can also be authenticated using a valid token credential. With this option, , and environment variables need to be set up for authentication.
AZURE_CLIENT_SECRETAZURE_CLIENT_IDAZURE_TENANT_IDstring endpoint = "<endpoint_url>";
TokenCredential tokenCredential = new DefaultAzureCredential();
SmsClient client = new SmsClient(new Uri(endpoint), tokenCredential);To send a SMS message, call the Send or SendAsync function from the SmsClient.
SmsSendResult sendResult = await smsClient.SendAsync(
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
to: "<to-phone-number>", // E.164 formatted recipient phone number
message: "Hi");
Console.WriteLine($"Sms id: {sendResult.MessageId}");To send a SMS message to a list of recipients, call the Send or SendAsync function from the SmsClient with a list of recipient's phone numbers.
You can also provide an options object to configure various settings, such as enabling the delivery report, adding custom tags, or specifying parameters for connecting with the Messaging Connect Partner to deliver SMS.
var response = await smsClient.SendAsync(
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }, // E.164 formatted recipient phone numbers
message: "Weekly Promotion!",
options: new SmsSendOptions(enableDeliveryReport: true) // OPTIONAL
{
Tag = "marketing", // custom tags
DeliveryReportTimeoutInSeconds = 90
});
foreach (SmsSendResult result in response.Value)
{
Console.WriteLine($"Sms id: {result.MessageId}");
Console.WriteLine($"Send Result Successful: {result.Successful}");
}To check if the recipients are in the Opt Out list, call the function from the SmsClient.OptOuts.Check or SmsClient.OptOuts.CheckAsync with a list of recipient phone numbers.
var optOutCheckResults = await smsClient.OptOuts.CheckAsync(
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
foreach (var result in optOutCheckResults.Value)
{
Console.WriteLine($"{result.To}: {result.IsOptedOut}");
}To add the list of recipients to Opt Out list, call the function from the SmsClient.OptOuts.Add or SmsClient.OptOuts.AddAsync with a list of recipient phone numbers.
var optOutAddResults = await smsClient.OptOuts.AddAsync(
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
foreach (var result in optOutAddResults.Value)
{
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
}To remove the list of recipients to Opt Out list, call the function from the SmsClient.OptOuts.Remove or SmsClient.OptOuts.RemoveAsync with a list of recipient phone numbers.
var optOutRemoveResults = await smsClient.OptOuts.RemoveAsync(
from: "<from-phone-number>", // Your E.164 formatted from phone number used to send SMS
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" }); // E.164 formatted recipient phone numbers
foreach (var result in optOutRemoveResults.Value)
{
Console.WriteLine($"{result.To}: {result.HttpStatusCode}");
}SMS operations will throw an exception if the request to the server fails.
Exceptions will not be thrown if the error is caused by an individual message, only if something fails with the overall request.
Please use the Successful flag to validate each individual result to verify if the message was sent.
try
{
var response = await smsClient.SendAsync(
from: "<from-phone-number>" // Your E.164 formatted phone number used to send SMS
to: new string [] {"<to-phone-number-1>", "<to-phone-number-2>"}, // E.164 formatted recipient phone number
message: "Weekly Promotion!",
options: new SmsSendOptions(enableDeliveryReport: true) // OPTIONAL
{
Tag = "marketing", // custom tags
});
foreach (SmsSendResult result in response.Value)
{
if (result.Successful)
{
Console.WriteLine($"Successfully sent this message: {result.MessageId} to {result.To}.");
}
else
{
Console.WriteLine($"Something went wrong when trying to send this message {result.MessageId} to {result.To}.");
Console.WriteLine($"Status code {result.HttpStatusCode} and error message {result.ErrorMessage}.");
}
}
}
catch (RequestFailedException ex)
{
Console.WriteLine(ex.Message);
}This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.