ارسال پیام‌ها با استفاده از کلاینت سرویس 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);

زمان‌بندی پیام و افزودن دسته‌ها

برای زمان‌بندی تحویل، یک 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());
}