OJ Develops

Thoughts on software development. .NET | C# | Azure

Sending Email With Azure

14 January 2023

Azure provides free resources for email sending, namely:

  • Communication Services
  • Email Communication Services
  • Email Communication Services Domain

I will provide a step-by-step guide with screenshots on how to send email using those resources together with the associated .NET SDK.

Create Azure Resources

Create a Communication Services Resource

  1. Search for Communication Services and create the resource.

communication-services-card

  1. Under the Instance Details section, there is a selection for Data location. We must choose United States for this to work. I will explain why when we get to creating the Email Communication Services resource.

create-communication-services

  1. Fill the other required fields and create the resource.

Create an Email Communication Services resource

  1. Search for Email Communication Services and create the resource. At the time of writing, it is still under Preview. However, I found that it’s already working as expected.

email-communication-services-card

  1. Similar to Communication Services, there is also a a selection for Data location under the Instance Details section. Note that the only selection possible is United States.

For email sending the work, the data location of our Communication Service must match the data location of our Email Communication Service. That’s why we had to choose United States when we created the Communication Service earlier. Hopefully, more data location options will be added in the future.

create-email-communication-services

  1. Fill the other required fields and create the resource.

Create an Email Communication Services Domain

In the same way that Azure provides a free out-of-the-box domain for Azure App Services web sites, Azure also provides a free out-of-the-box domain for emails. We are going to use the provided domain, which is @{identifier}.azurecomm.net, where identifier is a placeholder for a random identifier.

We need to tell Azure that we want to use the provided domain, as opposed to, say, a custom domain that we already own.

  1. Open the Email Communication Services resource and click Provision domains on the sidebar.

provision-domains-menu

  1. Under Add a free Azure subdomain, click on the 1-click add button.

provision-domains-content

The domain will be deployed in the background, which can take a few minutes. After deployment finishes, the new domain will be shown on the screen.

provision-domains-list

Note that the domain is represented not just as a property on the Email Communication Services resource but as an entirely new resource altogether, which is the Email Communication Services Domain resource. Clicking on the domain name from the list will take us to the resource.

Connect the email domain to Communication Services

The final step on the Azure side of things is to connect the email domain with our Communication Services resource.

  1. Open the Communication Services resource and click Domains under Email on the sidebar. You may have to scroll down a little bit to see it.

email-domains-menu

  1. Click on Connect domain.

email-domains-content

  1. The Connect email domains blade will appear on the right. Choose the Email Service and Verified Domain that we set up earlier in the process. If the email service dropdown is empty, that might mean that the data location for the Communication Service and Email Communication Service resources are different.

connect-email-domains

  1. Click Connect. After it finishes connecting, the domain will appear on the Domains page.

email-domains-list

We also need some information that will be used later during coding. Retrieve the connection string of the Communication Services resource under the Keys menu on the sidebar:

communication-services-keys

Also retrieve the MailFrom address which can be found in the Email Communication Services Domain resource on the Overview page:

domain-overview

We now have all the necessary Azure resources and information to send email.

Send Email using the .NET SDKs

SDKs for different languages are available, including C#, JavaScript, Java, and Python. We will be using the C# SDK.

  1. Install the Azure.Communication.Email package.
dotnet add package Azure.Communication.Email --prerelease

Note that at the time of writing, the package does not appear in Visual Studio’s package manager, even when the prerelease box is checked.

  1. Send an email using the following code, replacing some parts with your own text:
using Azure;
using Azure.Communication.Email;
using Azure.Communication.Email.Models;

// ...

var connectionString = ""; // put the connection string of the communication service resource
var mailFrom = ""; // put the mailfrom address from the domain resource

EmailClient emailClient = new EmailClient(connectionString);
EmailContent emailContent = new EmailContent("-put subject here-");
emailContent.PlainText = "-put body here-";
List<EmailAddress> emailAddresses = new List<EmailAddress> { new EmailAddress("-put recipient email here-") { DisplayName = "-put recipient name here-" }};
EmailRecipients emailRecipients = new EmailRecipients(emailAddresses);
EmailMessage emailMessage = new EmailMessage(mailFrom, emailContent, emailRecipients); 
SendEmailResult emailResult = emailClient.Send(emailMessage,CancellationToken.None);

That’s it.

More Information

Versions used: .NET 6; Azure.Communication.Email package 1.0.0-beta.1