Skip to content

Step 3: Send Email

This step covers sending transactional emails using the EmailClient.

Prerequisites

  • An Azure Communication Services resource.
  • An Azure Email Communication Services resource linked to your ACS resource.
  • A verified domain and a "From" email address.

1. Add Email NuGet Package

dotnet add package Azure.Communication.Email

2. Initialize EmailClient

using Azure.Communication.Email;

string connectionString = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_CONNECTION_STRING");
EmailClient emailClient = new EmailClient(connectionString);

3. Send a Simple Email

public async Task SendEmail()
{
    var emailMessage = new EmailMessage(
        senderAddress: "do-not-reply@yourdomain.com",
        recipientAddress: "user@example.com",
        subject: "Welcome to ACS",
        htmlContent: "<html><h1>Hello!</h1><p>This is a test email from .NET.</p></html>"
    );

    EmailSendOperation emailSendOperation = await emailClient.SendAsync(
        WaitUntil.Completed,
        emailMessage);

    Console.WriteLine($"Email status: {emailSendOperation.Value.Status}");
}

4. Send with Attachments

public async Task SendEmailWithAttachment()
{
    var content = await File.ReadAllBytesAsync("report.pdf");
    var attachment = new EmailAttachment(
        "report.pdf",
        "application/pdf",
        new BinaryData(content)
    );

    var emailMessage = new EmailMessage(
        senderAddress: "sender@yourdomain.com",
        recipientAddress: "user@example.com",
        subject: "Monthly Report",
        plainTextContent: "Please find the attached report.");

    emailMessage.Attachments.Add(attachment);

    await emailClient.SendAsync(WaitUntil.Completed, emailMessage);
}

5. Polling for Status

In .NET, you can use WaitUntil.Completed to block until the email is sent, or WaitUntil.Started to get an operation ID for later polling.

EmailSendOperation operation = await emailClient.SendAsync(WaitUntil.Started, emailMessage);
string operationId = operation.Id;
Console.WriteLine($"Operation ID: {operationId}");

Full Code Example

using System;
using System.IO;
using System.Threading.Tasks;
using Azure;
using Azure.Communication.Email;

class Program
{
    static async Task Main(string[] args)
    {
        string connectionString = Environment.GetEnvironmentVariable("COMMUNICATION_SERVICES_CONNECTION_STRING");
        var emailClient = new EmailClient(connectionString);

        var emailMessage = new EmailMessage(
            senderAddress: "donotreply@yourdomain.com",
            recipientAddress: "user@example.com",
            subject: ".NET Email Quickstart",
            plainTextContent: "This is the body of the email.");

        var operation = await emailClient.SendAsync(WaitUntil.Completed, emailMessage);
        Console.WriteLine($"Email sent with ID: {operation.Id}");
    }
}

Next Step

Build real-time features with Chat.

Sources