Delen via


Azure Communication E-mailclientbibliotheek voor JavaScript - versie 1.1.0

Dit pakket bevat een JavaScript/TypeScript SDK voor Azure Communication Services for Email.

Aan de slag

Vereiste voorwaarden

U hebt een Azure-abonnement, een communicatieserviceresource en een e-mailcommunicatieresource met een actief domein nodig.

Als u deze resources wilt maken, kunt u de Azure Portal, de Azure PowerShell of de .NET-beheerclientbibliotheek gebruiken.

Installeren

npm install @azure/communication-email

Voorbeelden

EmailClient Biedt de functionaliteit om e-mailberichten te verzenden.

Authenticatie

E-mailclients kunnen worden geverifieerd met behulp van de verbindingsreeks die is verkregen van een Azure-communicatieresource in de 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);

U kunt ook verifiëren met Azure Active Directory met behulp van de Azure Identity-bibliotheek. Als u de DefaultAzureCredential-provider wilt gebruiken die hieronder wordt weergegeven, of andere referentieproviders die bij de Azure SDK zijn geleverd, installeert u het @azure/identity pakket:

npm install @azure/identity

Het @azure/identity-pakket biedt verschillende referentietypen die uw toepassing hiervoor kan gebruiken. De README voor @azure/identity biedt meer details en voorbeelden om u op weg te helpen. AZURE_CLIENT_SECRET-, AZURE_CLIENT_ID- en AZURE_TENANT_ID-omgevingsvariabelen zijn nodig om een DefaultAzureCredential-object te maken.

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

const endpoint = "https://<resource-name>.communication.azure.com";
const credential = new DefaultAzureCredential();
const client = new EmailClient(endpoint, credential);

Stuur een e-mailbericht

Als u een e-mailbericht wilt verzenden, roept u de functie beginSend aan vanuit de EmailClient. Dit zal een poller retourneren. U kunt deze poller gebruiken om de status van de bewerking te controleren en het resultaat op te halen zodra deze is voltooid.

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

const endpoint = "https://<resource-name>.communication.azure.com";
const credential = new DefaultAzureCredential();
const client = new EmailClient(endpoint, credential);

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

const poller = await client.beginSend(message);
const response = await poller.pollUntilDone();

Een e-mailbericht verzenden naar meerdere ontvangers

Als u een e-mailbericht naar meerdere geadresseerden wilt verzenden, voegt u een object toe voor elk type geadresseerde en een object voor elke geadresseerde.

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

const endpoint = "https://<resource-name>.communication.azure.com";
const credential = new DefaultAzureCredential();
const client = new EmailClient(endpoint, credential);

const message = {
  senderAddress: "sender@contoso.com",
  content: {
    subject: "This is the subject",
    plainText: "This is the body",
  },
  recipients: {
    to: [
      {
        address: "customer1@domain.com",
        displayName: "Customer Name 1",
      },
      {
        address: "customer2@domain.com",
        displayName: "Customer Name 2",
      },
    ],
    cc: [
      {
        address: "ccCustomer1@domain.com",
        displayName: " CC Customer 1",
      },
      {
        address: "ccCustomer2@domain.com",
        displayName: "CC Customer 2",
      },
    ],
    bcc: [
      {
        address: "bccCustomer1@domain.com",
        displayName: " BCC Customer 1",
      },
      {
        address: "bccCustomer2@domain.com",
        displayName: "BCC Customer 2",
      },
    ],
  },
};

const poller = await client.beginSend(message);
const response = await poller.pollUntilDone();

E-mail met bijlagen verzenden

Azure Communication Services ondersteunt het verzenden van e-mail met bijlagen.

import { DefaultAzureCredential } from "@azure/identity";
import { EmailClient } from "@azure/communication-email";
import { basename } from "node:path";
import { readFileSync } from "node:fs";

const endpoint = "https://<resource-name>.communication.azure.com";
const credential = new DefaultAzureCredential();
const client = new EmailClient(endpoint, credential);

const filePath = "path/to/readme.txt";

const message = {
  senderAddress: "sender@contoso.com",
  content: {
    subject: "This is the subject",
    plainText: "This is the body",
  },
  recipients: {
    to: [
      {
        address: "customer@domain.com",
        displayName: "Customer Name",
      },
    ],
  },
  attachments: [
    {
      name: basename(filePath),
      contentType: "text/plain",
      contentInBase64: readFileSync(filePath, "base64"),
    },
  ],
};

const poller = await client.beginSend(message);
const response = await poller.pollUntilDone();

E-mail verzenden met inline bijlagen

Azure Communication Services ondersteunt het verzenden van e-mail met inline bijlagen. Als u een optionele contentId parameter aan een attachment toevoegt, wordt het een inline-bijlage.

import { DefaultAzureCredential } from "@azure/identity";
import { EmailClient } from "@azure/communication-email";
import { readFileSync } from "node:fs";

const endpoint = "https://<resource-name>.communication.azure.com";
const credential = new DefaultAzureCredential();
const client = new EmailClient(endpoint, credential);

const imageBuffer = readFileSync("path/to/my_inline_image.jpg");
const contentInBase64 = imageBuffer.toString("base64");

const message = {
  senderAddress: "sender@contoso.com",
  content: {
    subject: "This is the subject",
    plainText: "This is the body",
    html: '<html>This is the body<br /><img src="cid:inline_image" /></html>',
  },
  recipients: {
    to: [
      {
        address: "customer@domain.com",
        displayName: "Customer Name",
      },
    ],
  },
  attachments: [
    {
      name: "my_inline_image.jpg",
      contentType: "image/jpeg",
      contentInBase64: contentInBase64,
      contentId: "inline_image",
    },
  ],
};

const poller = await client.beginSend(message);
const response = await poller.pollUntilDone();

Probleemoplossingsproces

Loggen

Het inschakelen van logboekregistratie kan helpen nuttige informatie over fouten te ontdekken. Als u een logboek met HTTP-aanvragen en -antwoorden wilt zien, stelt u de omgevingsvariabele AZURE_LOG_LEVEL in op info. U kunt logboekregistratie ook tijdens runtime inschakelen door setLogLevel aan te roepen in de @azure/logger:

import { setLogLevel } from "@azure/logger";

setLogLevel("info");

Volgende stappen

Contributing

Dit project verwelkomt bijdragen en suggesties. Voor de meeste bijdragen moet u akkoord gaan met een Licentieovereenkomst voor inzenders (CLA) waarin wordt aangegeven dat u het recht hebt om, en daadwerkelijk, ons de rechten te verlenen om uw bijdrage te gebruiken. Ga voor meer informatie naar cla.microsoft.com.

Dit project onderschrijft de Microsoft Open Source gedragscode. Zie de Veelgestelde vragen over gedragscodes voor meer informatie of neem contact op met opencode@microsoft.com met eventuele aanvullende vragen of opmerkingen.