Sample code for 30+ languages & platforms
Java

Amazon SES v2: Send private appointment reminders and inspect each result

See more Amazon SES Examples

Replace the two recipients with test mailboxes you control. The example supplies an inline template and one private destination per entry. Store each successful message ID against its appointment. Schedule a retry with backoff only for entries with a retryable outcome. Correct permanent errors before resubmission.

SendBulkEmail · Per-entry outcomes

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.AuthAws;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.Socket;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // Assumes Chilkat is unlocked. See Global Unlock Sample.
        success = false;

        JsonObject request = new JsonObject();
        request.updateString("FromEmailAddress", "sender@example.com");
        request.updateString("DefaultContent.Template.TemplateContent.Subject", "Appointment reminder");
        request.updateString("DefaultContent.Template.TemplateContent.Text", "Hello {{name}}, your appointment is {{slot}}.");
        request.updateString("DefaultContent.Template.TemplateData", "{\"name\":\"there\",\"slot\":\"in your booking confirmation\"}");

        JsonObject firstReminder = new JsonObject();
        firstReminder.updateString("name", "Avery");
        firstReminder.updateString("slot", "Tuesday at 9 AM Central");
        request.updateString("BulkEmailEntries[0].Destination.ToAddresses[0]", "avery@example.com");
        request.updateString("BulkEmailEntries[0].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData", firstReminder.emit());

        JsonObject secondReminder = new JsonObject();
        secondReminder.updateString("name", "Robin");
        secondReminder.updateString("slot", "Wednesday at 2 PM Central");
        request.updateString("BulkEmailEntries[1].Destination.ToAddresses[0]", "robin@example.com");
        request.updateString("BulkEmailEntries[1].ReplacementEmailContent.ReplacementTemplate.ReplacementTemplateData", secondReminder.emit());
        // Use the same AWS Region for the identity, endpoint, and Signature Version 4.
        String region = "us-west-2";
        StringBuilder host = new StringBuilder();
        host.append("email.");
        host.append(region);
        host.append(".amazonaws.com");

        AuthAws signer = new AuthAws();
        // Supply the credentials from your application's credential provider.
        signer.setAccessKey("AWS_ACCESS_KEY_ID");
        signer.setSecretKey("AWS_SECRET_ACCESS_KEY");
        signer.setRegion(region);
        signer.setServiceName("ses");

        Socket connection = new Socket();
        connection.setRequireSslCertVerify(true);
        try {
            connection.connect(host.getAsString(), 443, true, 10000);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Rest rest = new Rest();
        try {
            rest.useConnection(connection, false);

            rest.setAuthAws(signer);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // For temporary AWS credentials, supply the session token from the same provider.
        String sessionToken = "";
        if (!"".equals(sessionToken)) {
            rest.addQueryParam("X-Amz-Security-Token", sessionToken);
        }

        rest.addHeader("Content-Type", "application/json");
        String responseText;
        try {
            responseText = rest.fullRequestString("POST", "/v2/email/outbound-bulk-emails", request.emit());
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        if (rest.getResponseStatusCode() != 200) {
            System.out.println("SES returned HTTP " + rest.getResponseStatusCode() + " " + responseText);
            return;
        }

        JsonObject response = new JsonObject();
        try {
            response.load(responseText);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        int count = response.sizeOfArray("BulkEmailEntryResults");
        if (count != request.sizeOfArray("BulkEmailEntries")) {
            System.out.println("Incomplete results. Reconcile the original batch before retrying.");
            return;
        }

        int i = 0;
        while (i < count) {
            response.setI(i);
            request.setI(i);
            String recipient = request.stringOf("BulkEmailEntries[i].Destination.ToAddresses[0]");
            String status = response.stringOf("BulkEmailEntryResults[i].Status");
            if ("SUCCESS".equals(status)) {
                System.out.println(recipient + " accepted: " + response.stringOf("BulkEmailEntryResults[i].MessageId"));
            } else {
                if (("ACCOUNT_THROTTLED".equals(status)) || ("TRANSIENT_FAILURE".equals(status))) {
                    System.out.println(recipient + " retry later: " + status);
                } else {
                    System.out.println(recipient + " review before retry: " + status + " " + response.stringOf("BulkEmailEntryResults[i].Error"));
                }
            }

            i = i + 1;
        }
    }
}