Azure Communication Email client library for .NET

This package contains a C# SDK for Azure Communication Services for Email. Source code | Package (NuGet) | Product documentation Install the Azure Communication Email client library for .NET with NuGet:

This package contains a C# SDK for Azure Communication Services for Email.

Source code | Package (NuGet) | Product documentation

Getting started

Install the package

Install the Azure Communication Email client library for .NET with NuGet:

dotnet add package Azure.Communication.Email --prerelease 

Prerequisites

You need an Azure subscription, a Communication Service Resource, and an Email Communication Resource with an active Domain.

To create these resource, you can use the Azure Portal, the Azure PowerShell, or the .NET management client library.

Key concepts

EmailClient provides the functionality to send email messages .

Using statements

using Azure.Communication.Email; 

Authenticate the client

Email 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 EmailClient emailClient = new EmailClient(connectionString); 

Alternatively, Email clients can also be authenticated using a valid token credential. With this option, AZURE_CLIENT_SECRET, AZURE_CLIENT_ID and AZURE_TENANT_ID environment variables need to be set up for authentication.

string endpoint = "<endpoint_url>"; TokenCredential tokenCredential = new DefaultAzureCredential(); tokenCredential = new DefaultAzureCredential(); EmailClient emailClient = new EmailClient(new Uri(endpoint), tokenCredential); 

Examples

Send a simple email message with automatic polling for status

To send an email message, call the simple overload of Send or SendAsync function from the EmailClient.

var emailSendOperation = emailClient.Send( wait: WaitUntil.Completed, from: "<Send email address>" // The email address of the domain registered with the Communication Services resource to: "<recipient email address>" subject: "This is the subject", htmlContent: "<html><body>This is the html body</body></html>"); Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}"); /// Get the OperationId so that it can be used for tracking the message for troubleshooting string operationId = emailSendOperation.Id; Console.WriteLine($"Email operation id = {operationId}"); 

Send a simple email message with manual polling for status

To send an email message, call the simple overload of Send or SendAsync function from the EmailClient.

/// Send the email message with WaitUntil.Started var emailSendOperation = await emailClient.SendAsync( wait: WaitUntil.Started, from: "<Send email address>" // The email address of the domain registered with the Communication Services resource to: "<recipient email address>" subject: "This is the subject", htmlContent: "<html><body>This is the html body</body></html>"); /// Call UpdateStatus on the email send operation to poll for the status /// manually. while (true) { await emailSendOperation.UpdateStatusAsync(); if (emailSendOperation.HasCompleted) { break; } await Task.Delay(100); } if (emailSendOperation.HasValue) { Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}"); } /// Get the OperationId so that it can be used for tracking the message for troubleshooting string operationId = emailSendOperation.Id; Console.WriteLine($"Email operation id = {operationId}"); 

Send an email message with more options

To send an email message, call the overload of Send or SendAsync function from the EmailClient that takes an EmailMessage parameter.

// Create the email content var emailContent = new EmailContent("This is the subject") { PlainText = "This is the body", Html = "<html><body>This is the html body</body></html>" }; // Create the EmailMessage var emailMessage = new EmailMessage( fromAddress: "<Send email address>" // The email address of the domain registered with the Communication Services resource toAddress: "<recipient email address>" content: emailContent); var emailSendOperation = emailClient.Send( wait: WaitUntil.Completed, message: emailMessage); Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}"); /// Get the OperationId so that it can be used for tracking the message for troubleshooting string operationId = emailSendOperation.Id; Console.WriteLine($"Email operation id = {operationId}"); 

Send an email message to multiple recipients

To send an email message to multiple recipients, add an EmailAddress object for each recipent type to the EmailRecipient object.

// Create the email content var emailContent = new EmailContent("This is the subject") { PlainText = "This is the body", Html = "<html><body>This is the html body</body></html>" }; // Create the To list var toRecipients = new List<EmailAddress> { new EmailAddress( address: "<recipient email address>" displayName: "<recipient displayname>" new EmailAddress( address: "<recipient email address>" displayName: "<recipient displayname>" }; // Create the CC list var ccRecipients = new List<EmailAddress> { new EmailAddress( address: "<recipient email address>" displayName: "<recipient displayname>" new EmailAddress( address: "<recipient email address>" displayName: "<recipient displayname>" }; // Create the BCC list var bccRecipients = new List<EmailAddress> { new EmailAddress( address: "<recipient email address>" displayName: "<recipient displayname>" new EmailAddress( address: "<recipient email address>" displayName: "<recipient displayname>" }; var emailRecipients = new EmailRecipients(toRecipients, ccRecipients, bccRecipients); // Create the EmailMessage var emailMessage = new EmailMessage( senderAddress: "<Send email address>" // The email address of the domain registered with the Communication Services resource emailRecipients, emailContent); EmailSendOperation emailSendOperation = emailClient.Send(WaitUntil.Completed, emailMessage); Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}"); /// Get the OperationId so that it can be used for tracking the message for troubleshooting string operationId = emailSendOperation.Id; Console.WriteLine($"Email operation id = {operationId}"); 

Send email with attachments

Azure Communication Services support sending emails with attachments.

// Create the EmailMessage var emailMessage = new EmailMessage( fromAddress: "<Send email address>" // The email address of the domain registered with the Communication Services resource toAddress: "<recipient email address>" content: emailContent); var filePath = "<path to your file>"; var attachmentName = "<name of your attachment>"; var contentType = MediaTypeNames.Text.Plain; var content = new BinaryData(System.IO.File.ReadAllBytes(filePath)); var emailAttachment = new EmailAttachment(attachmentName, contentType, content); emailMessage.Attachments.Add(emailAttachment); EmailSendOperation emailSendOperation = emailClient.Send(WaitUntil.Completed, emailMessage); Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}"); /// Get the OperationId so that it can be used for tracking the message for troubleshooting string operationId = emailSendOperation.Id; Console.WriteLine($"Email operation id = {operationId}"); 

Troubleshooting

A RequestFailedException is thrown as a service response for any unsuccessful requests. The exception contains information about what response code was returned from the service.

Next steps

Contributing

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.

ncG1vNJzZmiZqqq%2Fpr%2FDpJuom6Njr627wWeaqKqVY8SqusOorqxmnprBcHDWnplonJ%2Bpu6bAjnqxrqqVY5CwucyupaKbkam2sLqNfqSaoZxkfm98jWlkm52klnt1e8inm56wXp3Brrg%3D

 Share!