Gửi Thông điệp Bằng Client Dịch vụ SendGrid trong Java
SendGridClient từ com.aspose.email.services.sendgrid.client gói gửi tin qua SendGrid Web API. Client kết nối qua HTTP. Để biết cách chọn thư viện HTTP, xem HTTP Client cho Dịch vụ Thư.
Client này hoạt động với mô hình dữ liệu riêng của nó. Để gửi một MailMessage qua SendGrid, xem Gửi Email qua MailGun và SendGrid.
Gửi Tin
Để tạo client, truyền API key SendGrid của bạn vào builder. Sử dụng SendGridMessage.create để chuẩn bị một tin với một người nhận và thân HTML, sau đó truyền cho 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);
}
Cái send phương thức trả về giá trị của X-Message-Id header phản hồi, hoặc null nếu client không tìm thấy header này trong phản hồi. Các mẫu mã khác trên trang này sử dụng sendGrid biến được tạo ở đây.
Cài đặt Người Nhận và Nội Dung
Để đặt nhiều người nhận, tên hiển thị và cả nội dung văn bản và HTML, xây dựng tin từ các phần của nó. Người nhận được nhóm trong một SendGridPersonalization đối tượng:
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);
Thêm Tệp Đính Kèm
SendGridAttachment.fromBytes tạo tệp đính kèm từ dữ liệu tệp, tên tệp và loại nội dung:
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);
Sử Dụng Mẫu Động
Để gửi tin dựa trên mẫu động, đặt ID mẫu và truyền dữ liệu mẫu qua 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);
Lên Lịch Tin và Thêm Danh Mục
Để lên lịch gửi, truyền một dấu thời gian Unix tính bằng giây tới setSendAt. Để nhóm tin trong thống kê SendGrid, thêm các danh mục với 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);
Xử lý Lỗi
Khi SendGrid trả về trạng thái lỗi, client ném ra HttpClientException, chứa trạng thái và nội dung phản hồi. Ngoại lệ mở rộng java.io.IOException, mà client cũng ném ra khi kết nối thất bại.
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());
}