Java で SendGrid サービスクライアントを使用したメッセージ送信

SendGridClient から参照してください com.aspose.email.services.sendgrid.client パッケージは SendGrid Web API 経由でメッセージを送信します。クライアントは HTTP で接続します。HTTP ライブラリの選択方法は、 メールサービス用 HTTP クライアント.

このクライアントは独自のデータモデルで動作します。送信するには MailMessage SendGrid 経由で詳しくは、 MailGun と SendGrid を介したメール送信.

メッセージの送信

クライアントを作成するには、SendGrid API キーをビルダーに渡します。使用するのは SendGridMessage.create を返すことを前提にしています。1 人の受信者と HTML 本文でメッセージを作成し、次にそれを mailSend().send:

import com.aspose.email.services.sendgrid.client.SendGridClient;
import com.aspose.email.services.sendgrid.model.SendGridMessage;

try (SendGridClient sendGrid = SendGridClient.builder().apiKey("api-key").build()) {
    SendGridMessage message = SendGridMessage.create(
            "from@example.com", "to@example.com", "Weekly report", "<p>The report is ready.</p>");
    String messageId = sendGrid.mailSend().send(message);
    System.out.println(messageId);
}

この send メソッドが X-Message-Id レスポンスヘッダー、または null クライアントがレスポンスにこのヘッダーを見つけられない場合です。このページの他のコード例は sendGrid ここで作成された変数です。

受信者とコンテンツの設定

複数の受信者、表示名、テキストと HTML の両方のコンテンツを設定するには、パーツからメッセージを構築します。受信者は SendGridPersonalization オブジェクト:

import com.aspose.email.services.sendgrid.model.SendGridAddress;
import com.aspose.email.services.sendgrid.model.SendGridContent;
import com.aspose.email.services.sendgrid.model.SendGridMessage;
import com.aspose.email.services.sendgrid.model.SendGridPersonalization;

SendGridPersonalization recipients = new SendGridPersonalization("to@example.com", "Recipient Name");
recipients.addCc(new SendGridAddress("cc@example.com"));
recipients.addBcc(new SendGridAddress("bcc@example.com"));

SendGridMessage message = new SendGridMessage();
message.setFrom(new SendGridAddress("from@example.com", "Reports Team"));
message.setReplyTo(new SendGridAddress("reply@example.com"));
message.setSubject("Weekly report");
message.addPersonalization(recipients);
message.addContent(SendGridContent.text("The report is ready."));
message.addContent(SendGridContent.html("<p>The report is <b>ready</b>.</p>"));

sendGrid.mailSend().send(message);

添付ファイルの追加

SendGridAttachment.fromBytes ファイルデータ、ファイル名、コンテンツタイプから添付ファイルを作成します:

import com.aspose.email.services.sendgrid.model.SendGridAttachment;
import com.aspose.email.services.sendgrid.model.SendGridMessage;
import java.nio.file.Files;
import java.nio.file.Paths;

byte[] data = Files.readAllBytes(Paths.get("report.pdf"));

SendGridMessage message = SendGridMessage.create(
        "from@example.com", "to@example.com", "Weekly report", "<p>The report is attached.</p>");
message.addAttachment(SendGridAttachment.fromBytes(data, "report.pdf", "application/pdf"));

sendGrid.mailSend().send(message);

動的テンプレートの使用

動的テンプレートでメッセージを送信するには、テンプレート ID を設定し、テンプレートデータを介して SendGridPersonalization:

import com.aspose.email.services.sendgrid.model.SendGridAddress;
import com.aspose.email.services.sendgrid.model.SendGridMessage;
import com.aspose.email.services.sendgrid.model.SendGridPersonalization;
import java.util.HashMap;
import java.util.Map;

Map<String, Object> data = new HashMap<String, Object>();
data.put("name", "Recipient Name");

SendGridPersonalization recipients = new SendGridPersonalization("to@example.com");
recipients.setDynamicTemplateData(data);

SendGridMessage message = new SendGridMessage();
message.setFrom(new SendGridAddress("from@example.com"));
message.setTemplateId("d-1234567890");
message.addPersonalization(recipients);

sendGrid.mailSend().send(message);

メッセージのスケジューリングとカテゴリ追加

配信スケジュールを設定するには、Unix タイムスタンプ(秒)を setSendAtをスローします。SendGrid の統計でメッセージをグループ化するには、カテゴリを追加します: addCategory:

import com.aspose.email.services.sendgrid.model.SendGridMessage;

SendGridMessage message = SendGridMessage.create(
        "from@example.com", "to@example.com", "Weekly report", "<p>The report is ready.</p>");
message.setSendAt(System.currentTimeMillis() / 1000 + 3600);
message.addCategory("reports");

sendGrid.mailSend().send(message);

エラー処理

SendGrid がエラー状態を返すと、クライアントは HttpClientException、ステータスとレスポンスボディを含みます。この例外は java.io.IOException、接続失敗時にクライアントがスローする例外です。

import com.aspose.email.services.sendgrid.client.SendGridClient;
import com.aspose.email.services.sendgrid.model.SendGridMessage;
import com.aspose.ext.http.HttpClientException;

try (SendGridClient sendGrid = SendGridClient.builder().apiKey("invalid-api-key").build()) {
    sendGrid.mailSend().send(SendGridMessage.create(
            "from@example.com", "to@example.com", "Weekly report", "<p>The report is ready.</p>"));
} catch (HttpClientException e) {
    System.out.println(e.getStatusCode() + ": " + e.getResponseBody());
}