שליחת הודעות באמצעות לקוח שירות SendGrid ב‑Java
SendGridClient מן ה‑ com.aspose.email.services.sendgrid.client חבילה שולחת הודעות דרך ממשק ה‑Web API של SendGrid. הלקוח מתחבר דרך HTTP. למידע על בחירת ספריית HTTP, ראה לקוחות HTTP לשירותי דואר.
לקוח זה פועל עם מודל הנתונים שלו. כדי לשלוח MailMessage דרך SendGrid, ראה שליחת אימייל באמצעות MailGun ו‑SendGrid.
שליחת הודעה
ליצירת לקוח, העבר מפתח API של SendGrid לבונה. השתמש ב‑ 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 כותרת המענה, או null אם הלקוח אינו מוצא כותרת זו במענה. דוגמאות קוד אחרות בדף זה משתמשות ב‑ 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);
שימוש בתבניות דינמיות
כדי לשלוח הודעה על בסיס תבנית דינמית, הגדר את מזהה התבנית והעבר את נתוני התבנית דרך 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);
תזמון הודעה והוספת קטגוריות
כדי לתזמן משלוח, העבר חותמת זמן יוניקס בשניות ל‑ 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());
}