Bagikan melalui


Pustaka klien Email Azure Communication untuk JavaScript - versi 1.1.0

Paket ini berisi JavaScript/TypeScript SDK untuk Azure Communication Services untuk Email.

Memulai Langkah Pertama

Prasyarat

Anda memerlukan langganan Azure, Sumber Daya Layanan Komunikasi, dan Sumber Daya Komunikasi Email dengan Domain aktif.

Untuk membuat sumber daya ini, Anda dapat menggunakan Portal Microsoft Azure, Azure PowerShell, atau pustaka klien manajemen .NET.

Menginstal

npm install @azure/communication-email

Examples

EmailClient menyediakan fungsionalitas untuk mengirim pesan email.

Authentication

Klien email dapat diautentikasi menggunakan string koneksi yang diperoleh dari Azure Communication Resource di Portal Microsoft Azure.

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

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

Anda juga dapat mengautentikasi dengan Azure Active Directory menggunakan pustaka Azure Identity. Untuk menggunakan penyedia DefaultAzureCredential yang ditunjukkan di bawah ini, atau penyedia kredensial lain yang disediakan dengan Azure SDK, instal paket :

npm install @azure/identity

Paket @azure/identity menyediakan berbagai jenis kredensial yang dapat digunakan aplikasi Anda untuk melakukan ini. README untuk @azure/identity menyediakan lebih banyak detail dan contoh untuk membantu Anda memulai. AZURE_CLIENT_SECRET, AZURE_CLIENT_ID, dan variabel lingkungan AZURE_TENANT_ID diperlukan untuk membuat objek DefaultAzureCredential.

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);

Kirim Pesan Email

Untuk mengirim pesan email, panggil fungsi beginSend dari EmailClient. Ini akan mengembalikan poller. Anda dapat menggunakan poller ini untuk memeriksa status operasi dan mengambil hasilnya setelah selesai.

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();

Kirim pesan email ke beberapa penerima

Untuk mengirim pesan email ke beberapa penerima, tambahkan objek untuk setiap jenis penerima dan objek untuk setiap penerima.

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();

Kirim Email dengan Lampiran

Azure Communication Services mendukung pengiriman email dengan lampiran.

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();

Kirim Email dengan Lampiran Sebaris

Azure Communication Services mendukung pengiriman email dengan lampiran sebaris. Menambahkan parameter opsional contentId ke an attachment akan menjadikannya lampiran sebaris.

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();

Troubleshooting

Penebangan kayu

Mengaktifkan pengelogan dapat membantu menemukan informasi yang berguna tentang kegagalan. Untuk melihat log permintaan dan respons HTTP, atur variabel lingkungan AZURE_LOG_LEVEL ke info. Atau, pengelogan dapat diaktifkan saat runtime dengan memanggil setLogLevel di @azure/logger:

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

setLogLevel("info");

Langkah selanjutnya

Contributing

Proyek ini menyambut kontribusi dan saran. Sebagian besar kontribusi mengharuskan Anda menyetujui Perjanjian Lisensi Kontributor (CLA) yang menyatakan bahwa Anda memiliki hak untuk, dan benar-benar melakukannya, memberi kami hak untuk menggunakan kontribusi Anda. Untuk detailnya, kunjungi cla.microsoft.com.

Proyek ini telah mengadopsi Kode Etik Sumber Terbuka Microsoft. Untuk informasi selengkapnya, lihat Tanya Jawab Umum Kode Etik atau hubungi opencode@microsoft.com dengan pertanyaan atau komentar tambahan apa pun.