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

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

作業の開始

前提条件

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

パッケージを組み込む

BOM ファイルを含める

ライブラリの一般提供 (GA) バージョンに依存するには、azure-sdk-bom をプロジェクトに含めてください。 次のスニペットでは、{bom_version_to_target} プレースホルダーをバージョン番号に置き換えます。 BOM の詳細については、 AZURE SDK BOM README に関するページを参照してください。

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-sdk-bom</artifactId>
            <version>{bom_version_to_target}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

次に、バージョン タグのない依存関係セクションに直接依存関係を含めます。

<dependencies>
  <dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-communication-email</artifactId>
  </dependency>
</dependencies>

直接依存関係を含める

BOM に存在しないライブラリの特定のバージョンに依存する場合は、次のように直接依存関係をプロジェクトに追加します。

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-communication-email</artifactId>
    <version>1.0.7</version>
</dependency>

主要な概念

詳細は近日公開予定です。

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

クライアントの作成と認証

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

String connectionString = "https://<resource-name>.communication.azure.com/;<access-key>";

EmailClient emailClient = new EmailClientBuilder()
    .connectionString(connectionString)
    .buildClient();

Emailクライアントは、エンドポイントと Azure Portal の Azure Communication Resource から取得した Azure Key Credential を使用して作成および認証することもできます。

String endpoint = "https://<resource-name>.communication.azure.com";
AzureKeyCredential azureKeyCredential = new AzureKeyCredential("<access-key>");

EmailClient emailClient = new EmailClientBuilder()
    .endpoint(endpoint)
    .credential(azureKeyCredential)
    .buildClient();

Azure Active Directory トークン認証

DefaultAzureCredential オブジェクトは、credential() メソッドを使用して EmailClientBuilder に渡す必要があります。 エンドポイントは、endpoint() メソッドを使用して設定する必要もあります。

AZURE_CLIENT_SECRETAZURE_CLIENT_IDAZURE_TENANT_ID の環境変数は、DefaultAzureCredential オブジェクトを作成するために必要です。

// You can find your endpoint and access key from your resource in the Azure Portal
String endpoint = "https://<resource-name>.communication.azure.com/";

EmailClient emailClient = new EmailClientBuilder()
    .endpoint(endpoint)
    .credential(new DefaultAzureCredentialBuilder().build())
    .buildClient();

Email メッセージを送信する

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

EmailMessage message = new EmailMessage()
    .setSenderAddress("<sender-email-address>")
    .setToRecipients("<recipient-email-address>")
    .setSubject("test subject")
    .setBodyPlainText("test message");

SyncPoller<EmailSendResult, EmailSendResult> poller = emailClient.beginSend(message);
PollResponse<EmailSendResult> response = poller.waitForCompletion();

System.out.println("Operation Id: " + response.getValue().getId());

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

複数の受信者にメール メッセージを送信するには、適切な EmailMessage セッターに新しいアドレスを追加するだけです。

EmailMessage message = new EmailMessage()
    .setSenderAddress("<sender-email-address>")
    .setSubject("test subject")
    .setBodyPlainText("test message")
    .setToRecipients("<recipient-email-address>", "<recipient-2-email-address>")
    .setCcRecipients("<cc-recipient-email-address>")
    .setBccRecipients("<bcc-recipient-email-address>");

SyncPoller<EmailSendResult, EmailSendResult> poller = emailClient.beginSend(message);
PollResponse<EmailSendResult> response = poller.waitForCompletion();

System.out.println("Operation Id: " + response.getValue().getId());

電子メール メッセージの受信者をさらにカスタマイズするには、オブジェクトを EmailAddress インスタンス化し、それを適切な 'EmailMessage' セッターに渡します。

EmailAddress toAddress1 = new EmailAddress("<recipient-email-address>")
    .setDisplayName("Recipient");

EmailAddress toAddress2 = new EmailAddress("<recipient-2-email-address>")
    .setDisplayName("Recipient 2");

EmailMessage message = new EmailMessage()
    .setSenderAddress("<sender-email-address>")
    .setSubject("test subject")
    .setBodyPlainText("test message")
    .setToRecipients(toAddress1, toAddress2);

SyncPoller<EmailSendResult, EmailSendResult> poller = emailClient.beginSend(message);
PollResponse<EmailSendResult> response = poller.waitForCompletion();

System.out.println("Operation Id: " + response.getValue().getId());

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

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

BinaryData attachmentContent = BinaryData.fromFile(new File("C:/attachment.txt").toPath());
EmailAttachment attachment = new EmailAttachment(
    "attachment.txt",
    "text/plain",
    attachmentContent
);

EmailMessage message = new EmailMessage()
    .setSenderAddress("<sender-email-address>")
    .setToRecipients("<recipient-email-address>")
    .setSubject("test subject")
    .setBodyPlainText("test message")
    .setAttachments(attachment);

SyncPoller<EmailSendResult, EmailSendResult> poller = emailClient.beginSend(message);
PollResponse<EmailSendResult> response = poller.waitForCompletion();

System.out.println("Operation Id: " + response.getValue().getId());

トラブルシューティング

詳細は近日公開予定です。

次の手順

共同作成

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

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