你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
此包包含适用于电子邮件的 Azure 通信服务的 JavaScript/TypeScript SDK。
入门指南
先决条件
需要 Azure 订阅、通信服务资源和具有活动域的电子邮件通信资源。
若要创建这些资源,可以使用 Azure 门户、 Azure PowerShell 或 .NET 管理客户端库。
安装
npm install @azure/communication-email
例子
EmailClient 提供发送电子邮件的功能。
Authentication
可以使用从 Azure 门户中的 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);
还可以使用 Azure 标识库向 Azure Active Directory 进行身份验证。 若要使用如下所示的 DefaultAzureCredential 提供程序,或 Azure SDK 提供的其他凭据提供程序,请安装 @azure/identity 包:
npm install @azure/identity
@azure/identity 包提供了应用程序可用于执行此作的各种凭据类型。
@azure/identity 的自述文件提供了更多详细信息和示例来帮助你入门。
AZURE_CLIENT_SECRET、AZURE_CLIENT_ID和AZURE_TENANT_ID环境变量需要创建 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);
发送电子邮件
若要发送电子邮件,请从 beginSend调用 EmailClient 函数。 这将返回一个轮询器。 您可以使用此轮询器检查作的状态,并在作完成后检索结果。
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();
向多个收件人发送电子邮件
要向多个收件人发送电子邮件,请为每种收件人类型添加一个对象,并为每个收件人添加一个对象。
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();
使用附件发送电子邮件
Azure 通信服务支持发送带有附件的电子邮件。
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();
发送带有内联附件的电子邮件
Azure 通信服务支持发送带有内联附件的电子邮件。
向 an attachment 添加可选contentId参数将使其成为内联附件。
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
伐木业
启用日志记录可能有助于发现有关故障的有用信息。 若要查看 HTTP 请求和响应的日志,请将 AZURE_LOG_LEVEL 环境变量设置为 info。 或者,可以通过在 setLogLevel中调用 @azure/logger 在运行时启用日志记录:
import { setLogLevel } from "@azure/logger";
setLogLevel("info");
后续步骤
Contributing
此项目欢迎贡献和建议。 大多数贡献要求你同意参与者许可协议(CLA),声明你有权(实际这样做)授予我们使用你的贡献的权利。 有关详细信息,请访问 cla.microsoft.com。
该项目已采用 Microsoft开源行为准则。 有关详细信息,请参阅 行为准则常见问题解答 或与 opencode@microsoft.com 联系,了解任何其他问题或意见。