Java में SendGrid सेवा क्लाइंट का उपयोग करके संदेश भेजना
SendGridClient से com.aspose.email.services.sendgrid.client पैकेज SendGrid वेब API के माध्यम से संदेश भेजता है। क्लाइंट HTTP पर कनेक्ट होता है। HTTP लाइब्रेरी चुनने के लिए देखें मेल सेवाओं के लिए HTTP क्लाइंट्स.
यह क्लाइंट अपने स्वयं के डेटा मॉडल के साथ काम करता है। एक भेजने के लिए MailMessage SendGrid के माध्यम से, देखें MailGun और SendGrid के माध्यम से ईमेल भेजें.
संदेश भेजना
क्लाइंट बनाने के लिए, अपना SendGrid API कुंजी बिल्डर को पास करें। उपयोग करें 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);
डायनामिक टेम्पलेट्स का उपयोग
डायनामिक टेम्पलेट के आधार पर संदेश भेजने के लिए, टेम्पलेट 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 टाइमस्टैम्प पास करें 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());
}