Java で MailGun と SendGrid を使用してメールを送信する

MailGun と SendGrid を使用したメッセージ送信

Aspose.Email は MailGun または SendGrid サービスを使用してメールメッセージを送信するための統一 API を提供します。この API を使用すると、クライアントを初期化し、メールメッセージを作成して送信できます。

まず、どのサービスを使用してメッセージを送信するかに応じてオプションを設定することが重要です。 DeliveryServiceOptions class、DeliveryServiceClient パラメータを設定します。以下のコードサンプルは、サービスのオプション設定方法を示しています。

MailGun クライアント オプション:

String domain = "YOUR_MAILGUN_DOMEN";
String privApiKey = "YOUR_MAILGUN_PRIVATE_API_KEY";
MailgunClientOptions opt = new MailgunClientOptions();
opt.setDomain(domain);
opt.setApiKey(privApiKey);

SendGrid クライアント オプション:

String privApiKey = "YOUR_SENDGRID_PRIVATE_API_KEY";
SendGridClientOptions opt = new SendGridClientOptions();
opt.setApiKey(privApiKey);

次に、ビルダーを使用して必要なクライアントインスタンスを呼び出します。

IDeliveryServiceClient client = DeliveryServiceClientFactory.get(opt);

最後に、メールメッセージを作成して送信します。

MailMessage eml = new MailMessage("fromAddress", "toAddress", "subject", "body");

DeliveryServiceResponse resp = client.send(eml);

if (!resp.isSuccessful()) {
    for (String error : resp.getErrorMessages()) {
        System.out.println(error);
    }
}