การส่งข้อความโดยใช้ SendGrid Service Client ใน Java

SendGridClient จาก com.aspose.email.services.sendgrid.client แพคเกจส่งข้อความผ่าน SendGrid Web API. ไคลเอนต์เชื่อมต่อผ่าน HTTP. เพื่อเรียนรู้วิธีเลือกไลบรารี HTTP, ดู HTTP Clients สำหรับบริการอีเมล.

ไคลเอนต์นี้ทำงานกับโมเดลข้อมูลของตนเอง. เพื่อส่ง MailMessage ผ่าน SendGrid, ดู ส่งอีเมลผ่าน MailGun และ SendGrid.

การส่งข้อความ

เพื่อสร้างไคลเอนต์, ส่งคีย์ API ของ SendGrid ให้กับ builder. ใช้ SendGridMessage.create เพื่อเตรียมข้อความที่มีผู้รับหนึ่งคนและเนื้อหา 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 header ของคำตอบ, หรือ null หากไคลเอนต์หา header นี้ไม่พบในคำตอบ. ตัวอย่างโค้ดอื่นบนหน้านี้ใช้ 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);

การใช้เทมเพลตไดนามิก

เพื่อส่งข้อความโดยใช้เทมเพลตไดนามิก, ตั้งค่า template 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 timestamp หน่วยวินาทีให้ 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());
}