Send Emails with Azure Communication Services
Tested with:
- Azure Subscription
- az CLI 2.71.0
- Ubuntu 24.04
Microsoft offers the possibility to send emails from Azure without using third party services like Twilio SendGrid. The Azure Communication Services include the functionality needed too do that, in addition to SMS. They also support chats and video calls, but we will concentrate on emailing in this article.
Before anything, let’s define some env variables that we plan to use:
SUBSCRIPTION=<replace_with_your_subscription_id>
RESOURCE_GROUP_NAME=<replace_with_resource_group_name>
ECS=<replace_with_azure_communication_service_name>
ACS=<replace_with_azure_communication_service_name>
Remember that az has a pretty useful help section for its subcommands, e.g.:
az communication email -h
will display something like:
Group
az communication email : Manage Communication Email.
This command group is in preview and under development. Reference and support levels:
https://aka.ms/CLI_refstatus
Subgroups:
domain : Manage Communication Email Domain.
status : Commands to get the status of emails previously sent using Azure Communication Services
Email service.
Commands:
create : Create a new EmailService or update an existing EmailService.
delete : Delete to delete a EmailService.
list : List requests to list all resources in a subscription.
send : Send an email and get final status.
show : Get the EmailService and its properties.
update : Update a new EmailService or update an existing EmailService.
wait : Place the CLI in a waiting state until a condition is met.
To search AI knowledge base for examples, use: az find "az communication email"
The first step is to activate the az extension that allows us to create communication services via the CLI:
az extension add --name communication
Once we have the capability in place, we can create the actual ACS:
az communication create --name $ACS -g $RESOURCE_GROUP_NAME -l global --data-location "United States"
Next, we need to define an Email Communication Service (ECS) to handle the actual email sending. To do that, we execute:
az communication email create -n $ECS -g $RESOURCE_GROUP_NAME -l global --data-location "United States"
The ECS from the previous step doesn’t come with a predefined email account. Before using the email functionality, we must create an account. Azure offers two options: either use your own domain and configure it for emails, or create an Azure managed account with a custom azure domain that has the following form: <UUID>.<data-region>.azurecomm.net. For the sake of this article, we go with the latter account type, thus we run:
az communication email domain create -g $RESOURCE_GROUP_NAME --domain-name AzureManagedDomain --email-service-name $ECS --domain-management AzureManaged
Please keep in mind that by going with the managed option, the domain name (AzureManagedDomain) and the domain management (AzureManaged) are predefined and cannot be changed. Now that all the components are created, it’s time to connect them in this final step. There’s no particular command available to do that, but az CLI offers a generic update / add method that allows us to modify some Azure Communication Services properties by adding stuff to the existing values. Since ACS supports more than one domains, the ACS property linked_domains is a list.
az communication update -n $ACS -g $RESOURCE_GROUP_NAME --add linked_domains "/subscriptions/$SUBSCRIPTION/resourceGroups/$RESOURCE_GROUP_NAME/providers/Microsoft.Communication/emailServices/$ECS/domains/AzureManagedDomain"
To test the email functionality, we can also use the az CLI:
az communication email send --sender "<UUID>.<data-region>.azurecomm.net" --to "<destination_email>" --subject "Hello from ACS" --content "Testing email functionality." --connection-string "<your_connection_string>"
If you need to connect to an ACS from within a piece of code, you have to obtain a connection string. These rely on ACS keys, and Azure offers two of them, thus we have two connection string available, differentiated by the prefixes primaryand secondary,respectively. To get the priamry connection string, for an existing ACS, we execute the following command:
az communication list-key -g $RESOURCE_GROUP_NAME -n $ACS --query "primaryConnectionString" --output tsv
The response returned after running the command listed above contains more information such as the primary key, and the secondary key with its corresponding connection string.