이 패키지에는 이메일용 Azure Communication Services용 JavaScript/TypeScript SDK가 포함되어 있습니다.
시작하기
필수 조건
Azure 구독, 통신 서비스 리소스 및 활성 도메인이 있는 전자 메일 통신 리소스가 필요합니다.
이러한 리소스를 만들려면 Azure Portal, Azure PowerShell 또는 .NET 관리 클라이언트 라이브러리를 사용할 수 있습니다.
설치
npm install @azure/communication-email
예시
EmailClient 전자 메일 메시지를 보내는 기능을 제공합니다.
Authentication
전자 메일 클라이언트는 Azure Portal의 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 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";
const credential = new DefaultAzureCredential();
const client = new EmailClient(endpoint, credential);
이메일 메시지 보내기
전자 메일 메시지를 보내려면 beginSendEmailClient 함수를 호출합니다. 그러면 폴러가 반환됩니다. 이 폴러를 사용하여 작업 상태를 확인하고 완료되면 결과를 검색할 수 있습니다.
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 Communication Services는 첨부 파일이 있는 전자 메일 전송을 지원합니다.
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 Communication Services는 인라인 첨부 파일이 있는 전자 메일 전송을 지원합니다.
에 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 오픈 소스 준수 사항을 채택했습니다. 자세한 내용은 행동 강령 FAQ 참조하거나 추가 질문이나 의견을 opencode@microsoft.com 문의하세요.
Azure SDK for JavaScript