Skip to content

Email with Attachments

This recipe shows how to send emails with file attachments using Azure Communication Services (ACS) and JavaScript.

This recipe walks these steps in order:

flowchart TD
    START["Email with Attachments"]
    N1["Prerequisites"]
    N2["1. SDK Installation"]
    N3["2. Prepare Attachments"]
    N4["3. Send Email with Multiple Attachments"]
    N5["4. Attachment Size Limits"]
    N6["5. Multiple Attachment Types"]
    N7["6. Best Practices"]
    START --> N1
    N1 --> N2
    N2 --> N3
    N3 --> N4
    N4 --> N5
    N5 --> N6
    N6 --> N7

Prerequisites

1. SDK Installation

npm install @azure/communication-email

2. Prepare Attachments

Attachments must be base64-encoded and include a name and content type.

const fs = require("fs");
const path = require("path");

function prepareAttachment(filePath, contentType) {
    const fileContents = fs.readFileSync(filePath);
    const contentBytes = fileContents.toString("base64");

    return {
        name: path.basename(filePath),
        contentType: contentType,
        contentInBase64: contentBytes
    };
}

3. Send Email with Multiple Attachments

Include the prepared attachments in the attachments list of the message.

const { EmailClient } = require("@azure/communication-email");
const { DefaultAzureCredential } = require("@azure/identity");

// Initialize client
const endpoint = process.env.COMMUNICATION_SERVICES_ENDPOINT;
const emailClient = new EmailClient(endpoint, new DefaultAzureCredential());

async function sendEmailWithAttachments() {
  // Prepare attachments
  const attachment1 = prepareAttachment("document.pdf", "application/pdf");
  const attachment2 = prepareAttachment("image.png", "image/png");

  // Create email message
  const message = {
    senderAddress: "<verified-sender-email-address>",
    content: {
        subject: "ACS Email with Multiple Attachments",
        plainText: "See the attached files for your reference."
    },
    recipients: {
        to: [{ address: "<recipient-email-address>" }]
    },
    attachments: [attachment1, attachment2]
  };

  // Send email
  const poller = await emailClient.beginSend(message);
  const result = await poller.pollUntilDone();
  console.log(`Message ID: ${result.messageId}`);
}

sendEmailWithAttachments();

4. Attachment Size Limits

ACS has limits on the total size of an email and individual attachments.

  • Total Email Request Size: 10 MB (including attachments), per the current ACS service limits page. Higher limits (up to 30 MB) are available via Azure Support request.
  • Max Attachments: No strict limit, but bounded by total email size.

Important

Base64 encoding inflates message size by approximately 33%, so a nominal 10 MB request accommodates roughly 7.5 MB of pre-encoded content. For files above the request-size ceiling, store the file in Azure Blob Storage and include a SAS link in the email body instead. See reference/platform-limits.md for the authoritative repository reference.

5. Multiple Attachment Types

Common content types include: - application/pdf - image/png - image/jpeg - text/plain - application/vnd.openxmlformats-officedocument.wordprocessingml.document (Word doc)

6. Best Practices

  • Always validate file size before encoding to avoid exceeding limits.
  • Use meaningful attachment names.
  • Consider compression for large documents.

See Also

Sources