次の方法で共有


JavaScript 用 Azure Communication Email クライアント ライブラリ - バージョン 1.0.0

このパッケージには、Email用の JavaScript/TypeScript SDK for Azure Communication Servicesが含まれています。

作業の開始

前提条件

Azure サブスクリプションCommunication Service リソース、およびアクティブなドメインを持つEmail通信リソースが必要です。

これらのリソースを作成するには、Azure PortalAzure PowerShell、または .NET 管理クライアント ライブラリを使用できます。

[インストール中]

npm install @azure/communication-email

EmailClient は、電子メール メッセージを送信する機能を提供します。

認証

Emailクライアントは、Azure Portal の Azure Communication Resource から取得した接続文字列を使用して認証できます。

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

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

Azure ID ライブラリを使用して、Azure Active Directory で認証することもできます。 下に示した DefaultAzureCredential プロバイダーか、Azure SDK で提供されている他の資格情報プロバイダーを使用するには、@azure/identity パッケージをインストールしてください。

npm install @azure/identity

@azure/identity パッケージには、アプリケーションでこれを行うために使用できるさまざまな資格情報の種類が用意されています。 @azure/identity の README には、作業を開始するための詳細とサンプルが記載されています。 DefaultAzureCredential オブジェクトを作成するには、AZURE_CLIENT_SECRET、AZURE_CLIENT_ID、AZURE_TENANT_ID環境変数が必要です。

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

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

Email メッセージを送信する

メール メッセージを送信するには、EmailClient から beginSend 関数を呼び出します。 これにより、投票者が返されます。 このポーリングツールを使用すると、操作の状態をチェックし、完了したら結果を取得できます。

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 emailClient.beginSend(message);
const response = await poller.pollUntilDone();

複数の受信者にEmail メッセージを送信する

複数の受信者にメール メッセージを送信するには、受信者の種類ごとに オブジェクトを追加し、受信者ごとに 1 つのオブジェクトを追加します。

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 emailClient.beginSend(message);
const response = await poller.pollUntilDone();

添付ファイルを含むEmailを送信する

Azure Communication Servicesは、添付ファイルを含むメールの送信をサポートしています。

const filePath = "C://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: path.basename(filePath),
      contentType: "text/plain",
      contentInBase64: readFileSync(filePath, "base64"),
    },
  ],
};

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

次のステップ

共同作成

このプロジェクトでは、共同作成と提案を歓迎しています。 ほとんどの共同作成では、共同作成者使用許諾契約書 (CLA) にご同意いただき、ご自身の共同作成内容を使用する権利を Microsoft に供与する権利をお持ちであり、かつ実際に供与することを宣言していただく必要があります。 詳細については、「 cla.microsoft.com」を参照してください。

このプロジェクトでは、Microsoft オープン ソースの倫理規定を採用しています。 詳しくは、「Code of Conduct FAQ (倫理規定についてよくある質問)」を参照するか、opencode@microsoft.com 宛てに質問またはコメントをお送りください。