Azure Communication Services

In today's business landscape, email functionality plays a vital role in both marketing and business operations. With the increasing automation in CRM applications, there is a growing necessity for features that facilitate seamless communication, allowing businesses to effortlessly send emails to their customers. This article will provide a step-by-step guide on implementing email functionality in your CRM using the Azure Communication SDK.

Setup Azure Communication Service on Azure Portal

  • Go to the Azure Portal, search for Communication Service, and create a new Azure Communication Service resource named blog-email.

ACSEmailResource

  • After creating the service, it will take some time for deployment. Once completed, you'll be able to locate our new service under Communication Services with the name blog-email.

ACSList

  • Go to the blog-email service, where you will find the Try Email option; click on it.

SetupEmailService

  • You have the option to link an existing domain for sending emails or acquire a new domain from Azure for this purpose. Refer to this post for detailed instructions on setting up a custom domain: Setup Custom Domain.

  • In this blog post, we'll be establishing a free Azure Domain, and the initial step involves creating an email communication service resource. EmailCommunicationServiceDomain

  • After setting up the domain, it will be visible in the dropdown list, and the sender name will be listed as DoNotReply. sendemail

We've configured the Azure Communication service for sending emails. Now, let's see how to integrate it into our application.

Send emails in c# application

  • Install Azure.Communication.Email nuget package in your c# application

The following code snippet illustrates how to establish a connection to Azure Communication Service and send emails.

1using Azure;
2using Azure.Communication.Email;
3using System;
4namespace ACSDemo.Services
5{
6 public class EmailModel {
7 public string Recipient { get; set; }
8 public string Subject { get; set; }
9 public string HtmlContent { get; set; }
10 public string PlainText { get; set; }
11 }
12
13 public class EmailServices
14 {
15 private readonly string _smsConnection;
16 private readonly string _sender;
17 public EmailServices() {
18 // Get the connection string from portal
19 _smsConnection = Environment.GetEnvironmentVariable("SMSConnectionString");
20 // Get the sender email: DoNotReply@domain
21 _sender = Environment.GetEnvironmentVariable("SenderEmail");
22 }
23
24 private EmailClient GetEmailClient() {
25 // Get the Email Service client instance for connection string
26 return new EmailClient(_smsConnection);
27 }
28
29 public async Task SendEmailAsync(EmailModel model) {
30 var client= GetEmailClient();
31 // Send email
32 await client.SendAsync(
33 WaitUntil.Completed,
34 _sender,model.Recipient,model.Subject,model.HtmlContent,model.PlainText);
35
36 }
37 }
38}

This approach allows us to seamlessly integrate email functionality into our application.

References:

  1. Sending Emails with Attachments
  2. Azure Email Communication Services
  3. Add Azure Managed domain to Azure Email Communication Services