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);
    }
}