Berichten verzenden met de SendGrid‑service‑client in Java

SendGridClient van de com.aspose.email.services.sendgrid.client pakket verzendt berichten via de SendGrid‑Web‑API. De client maakt verbinding via HTTP. Zie hoe u een HTTP‑bibliotheek kiest in HTTP‑clients voor mail‑services.

Deze client werkt met eigen datamodel. Om een MailMessage via SendGrid, zie E‑mail verzenden via MailGun en SendGrid.

Een bericht verzenden

Om een client te maken, geef uw SendGrid‑API‑sleutel door aan de builder. Gebruik SendGridMessage.create om een bericht met één ontvanger en een HTML‑body voor te bereiden, en geef het vervolgens door aan 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);
}

De send methode retourneert de waarde van de X-Message-Id response‑header, of null als de client deze header niet kan vinden in de response. Andere code‑voorbeelden op deze pagina gebruiken de sendGrid variabele aangemaakt hier.

Ontvangers en inhoud instellen

Om meerdere ontvangers, weergavenamen en zowel tekst‑ als HTML‑inhoud in te stellen, bouw het bericht uit zijn delen. De ontvangers worden gegroepeerd in een SendGridPersonalization object:

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);

Bijlagen toevoegen

SendGridAttachment.fromBytes maakt een bijlage aan uit de bestandsdata, de bestandsnaam en het content‑type:

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);

Dynamische sjablonen gebruiken

Om een bericht te verzenden op basis van een dynamisch sjabloon, stel de sjabloon‑ID in en geef de sjabloongegevens door 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);

Bericht plannen en categorieën toevoegen

Om de levering te plannen, geef een Unix‑timestamp in seconden door aan setSendAt. Om berichten in SendGrid‑statistieken te groeperen, voeg categorieën toe met 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);

Fouten afhandelen

Wanneer SendGrid een foutstatus retourneert, gooit de client HttpClientException, die de status en de response‑body bevat. De exceptie breidt uit java.io.IOException, die de client ook gooit bij verbindingsfouten.

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());
}