Skicka meddelanden med SendGrid‑tjänsteklienten i Java
SendGridClient från com.aspose.email.services.sendgrid.client paket skickar meddelanden via SendGrid Web API. Klienten ansluter via HTTP. För att lära dig hur du väljer ett HTTP‑bibliotek, se HTTP‑klienter för e‑posttjänster.
Den här klienten arbetar med sin egen datamodell. För att skicka en MailMessage genom SendGrid, se Skicka e‑post via MailGun och SendGrid.
Skicka ett meddelande
För att skapa en klient, skicka din SendGrid‑API‑nyckel till byggaren. Använd SendGridMessage.create för att förbereda ett meddelande med en mottagare och HTML‑kropp, och sedan skicka det till 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);
}
Den send metoden returnerar värdet av X-Message-Id svarshuvudet, eller null om klienten inte kan hitta detta header‑fält i svaret. Andra kodexempel på den här sidan använder sendGrid variabel skapad här.
Ställa in mottagare och innehåll
För att ange flera mottagare, visningsnamn samt både text‑ och HTML‑innehåll, bygg meddelandet från dess delar. Mottagarna grupperas i ett SendGridPersonalization objekt:
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);
Lägga till bilagor
SendGridAttachment.fromBytes skapar en bilaga från fildata, filnamnet och content‑typen:
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);
Använda dynamiska mallar
För att skicka ett meddelande baserat på en dynamisk mall, ange mall‑ID och skicka mall‑data via 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);
Schemalägga ett meddelande och lägga till kategorier
För att schemalägga leverans, skicka ett Unix‑tidsstämpel i sekunder till setSendAt. För att gruppera meddelanden i SendGrid‑statistik, lägg till kategorier med 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);
Hantera fel
När SendGrid returnerar ett felstatus kastar klienten HttpClientException, som innehåller status och svarskropp. Undantaget ärver java.io.IOException, som klienten också kastar för anslutningsfel.
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());
}