إرسال رسائل باستخدام عميل خدمة SendGrid في جافا
SendGridClient من الـ com.aspose.email.services.sendgrid.client حزمة ترسل رسائل عبر واجهة ويب 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());
}