Send Email with Azure Communication Services (JavaScript)
The Azure Communication Services (ACS) Email client enables you to send transactional emails directly from your JavaScript or TypeScript applications.
Prerequisites
- An Azure subscription
- Azure Communication Services resource with Email capability enabled
- Node.js 14+ or compatible browser environment
Installation
npm install @azure/communication-email
Authentication
Retrieve the connection string or access key from the Azure portal and store it securely (e.g., Azure Key Vault or environment variable).
Basic Send Example
import { EmailClient } from "@azure/communication-email";
const connectionString = process.env.AZURE_COMMUNICATION_CONNECTION_STRING;
const emailClient = new EmailClient(connectionString);
async function sendEmail() {
const sendRequest = {
senderAddress: "no-reply@mydomain.com",
content: {
subject: "Welcome to Azure Email!",
plainText: "Hello,\\nYour account has been created successfully.",
html: "<h1>Hello,</h1><p>Your account has been created successfully.</p>"
},
recipients: {
to: [{ address: "user@example.com", displayName: "John Doe" }]
}
};
try {
const poller = await emailClient.beginSend(sendRequest);
const result = await poller.pollUntilDone();
console.log("Email sent! Message ID:", result.messageId);
} catch (e) {
console.error("Failed to send email:", e);
}
}
sendEmail();
Advanced: Attachments
import { EmailClient, EmailAttachment } from "@azure/communication-email";
const attachment: EmailAttachment = {
name: "welcome.pdf",
attachmentType: "application/pdf",
contentInBase64: "JVBERi0xLjQKJcfs... (base64 data)",
};
const requestWithAttachment = {
...sendRequest,
attachments: [attachment],
};
const poller = await emailClient.beginSend(requestWithAttachment);
await poller.pollUntilDone();
Live Demo
Enter your details to test sending an email (demo runs in the browser using a mock service).