Options
All
  • Public
  • Public/Protected
  • All
Menu

@azure/communication-email

Package version

Azure Communication Email client library for JavaScript

This package contains a JavaScript/TypeScript SDK for Azure Communication Services for Email.

Getting started

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.

Installing

npm install @azure/communication-email

Browser support

Key concepts

EmailClient provides the functionality to send email messages .

Examples

Authentication

Email clients can be authenticated using the connection string acquired from an Azure Communication Resource in the Azure Portal.

import { EmailClient } from "@azure/communication-email";

const connectionString = `endpoint=https://<resource-name>.communication.azure.com/;accessKey=<Base64-Encoded-Key>`;
const client = new EmailClient(connectionString);

Examples

Send an Email Message

To send an email message, call the send function from the EmailClient.

const emailMessage: EmailMessage = {
  sender: "sender@contoso.com",
  content: {
    subject: "This is the subject",
    plainText: "This is the body",
  },
  recipients: {
    to: [
      {
        email: "customer@domain.com",
        displayName: "Customer Name",
      },
    ],
  },
};

const response = await emailClient.send(emailMessage);

Send an Email Message to Multiple Recipients

To send an email message to multiple recipients, add a object for each recipient type and an object for each recipient.

const emailMessage: EmailMessage = {
  sender: "sender@contoso.com",
  content: {
    subject: "This is the subject",
    plainText: "This is the body",
  },
  recipients: {
    to: [
      {
        email: "customer1@domain.com",
        displayName: "Customer Name 1",
      },
      {
        email: "customer2@domain.com",
        displayName: "Customer Name 2",
      },
    ],
    cC: [
      {
        email: "ccCustomer1@domain.com",
        displayName: " CC Customer 1",
      },
      {
        email: "ccCustomer2@domain.com",
        displayName: "CC Customer 2",
      },
    ],
    bCC: [
      {
        email: "bccCustomer1@domain.com",
        displayName: " BCC Customer 1",
      },
      {
        email: "bccCustomer2@domain.com",
        displayName: "BCC Customer 2",
      },
    ],
  },
};

const response = await emailClient.send(emailMessage);

Send Email with Attachments

Azure Communication Services support sending email with attachments.

const filePath = "C://readme.txt";

const emailMessage: EmailMessage = {
  sender: "sender@contoso.com",
  content: {
    subject: "This is the subject",
    plainText: "This is the body",
  },
  recipients: {
    toRecipients: [
      {
        email: "customer@domain.com",
        displayName: "Customer Name",
      },
    ],
  },
  attachments: [
    {
      name: path.basename(filePath),
      attachmentType: "txt",
      contentBytesBase64: readFileSync(filePath, "base64"),
    },
  ],
};

const response = await emailClient.send(emailMessage);

Get Email Message Status

The result from the send call contains a messageId which can be used to query the status of the email.

const messageId = await emailClient.send(emailMessage);

const status = await emailClient.getSendStatus(messageId);

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.

Generated using TypeDoc