Exercise - Send an SMS using Azure Communication Services
In this unit, you'll use your Communication Services phone number to send an SMS message from a console application.
Create a console application
In a console window (such as cmd, PowerShell, or Bash), use the
dotnet new
command to create a new console app with the nameSmsQuickstart
. This command creates a simple "Hello World" C# project with a single source file:Program.cs
.dotnet new console -o SmsQuickstart
Change your directory to the newly created app folder and use the
dotnet build
command to compile your application.cd SmsQuickstart dotnet build
Add the SMS client library
While still in the application directory, install the Azure Communication Services SMS client library for .NET package by using the
dotnet add package
command.dotnet add package Azure.Communication.Sms --version 1.0.0-beta.3
Open the project by using the following code:
code .
Add a
using
directive to the top ofProgram.cs
to include theAzure.Communication
namespace.using Azure.Communication; using Azure.Communication.Sms;
Send an SMS
Replace the body of the
Main
method with code to initialize anSmsClient
with your connection string.string connectionString = <your-connection-string>; SmsClient smsClient = new SmsClient(connectionString);
Send an SMS message by calling the
Send
method. Add this code to the end ofMain
method inProgram.cs
:smsClient.Send( from: new PhoneNumber("<leased-phone-number>"), to: new PhoneNumber("<to-phone-number>"), message: "Hello World via SMS" );
Replace <leased-phone-number> with an SMS-enabled phone number you provisioned in the previous unit, and <to-phone-number> with the phone number you wish to send a message to.
Run the application from your application directory with the dotnet run command.
dotnet run