ส่งอีเมลผ่าน MailGun และ SendGrid ด้วย Java
Contents
[
Hide
]
ส่งข้อความโดยใช้ MailGun และ SendGrid
Aspose.Email ให้ API หนึ่งเดียวเพื่อส่งข้อความอีเมลโดยใช้บริการ MailGun หรือ SendGrid 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);
จากนั้น เรียกใช้อินสแตนซ์ของไคลเอนต์ที่ต้องการโดยใช้ builder
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);
}
}