Sample code for 30+ languages & platforms
Java Requires Chilkat v11.0.0+

SendGrid: Schedule a reminder and cancel it by batch ID

See more SendGrid Examples

Schedule a reminder one hour from now. Save its batch ID when it is allocated. If the appointment is canceled, use that saved ID in the cancellation branch. Mail Send accepts a Unix timestamp up to 72 hours ahead; cancel at least 10 minutes before the send time. Cancellation applies to the entire batch.

Create a batch ID ยท Cancel a batch

Chilkat Java Downloads

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

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

        // Assumes Chilkat is unlocked. See Global Unlock Sample.
        success = false;
        Http http = new Http();
        http.setRequireSslCertVerify(true);
        // Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
        http.setAuthToken("SENDGRID_API_KEY");
        HttpResponse resp = new HttpResponse();

        // On cancellation, set this to the batch ID saved with the original appointment.
        // This branch does not create a new batch or send another reminder.
        String batchToCancel = "";
        if (!"".equals(batchToCancel)) {
            JsonObject cancellation = new JsonObject();
            cancellation.updateString("batch_id", batchToCancel);
            cancellation.updateString("status", "cancel");
            try {
                http.httpJson("POST", "https://api.sendgrid.com/v3/user/scheduled_sends", cancellation, "application/json", resp);
            } catch (ChilkatException e) {
                if (resp.getStatusCode() == 0) {
                    System.out.println(e.getLastErrorText());
                } else {
                    System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
                }

                return;
            }

            if (resp.getStatusCode() != 201) {
                System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
                return;
            }

            System.out.println("Batch cancellation accepted.");
            return;
        }

        try {
            http.httpNoBody("POST", "https://api.sendgrid.com/v3/mail/batch", resp);
        } catch (ChilkatException e) {
            if (resp.getStatusCode() == 0) {
                System.out.println(e.getLastErrorText());
            } else {
                System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
            }

            return;
        }

        if (resp.getStatusCode() != 201) {
            System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
            return;
        }

        JsonObject batch = new JsonObject();
        try {
            batch.load(resp.getBodyStr());
        } catch (ChilkatException e) {
            System.out.println("Unexpected response: expected JSON.");
            return;
        }

        String batchId = batch.stringOf("batch_id");
        if (!batch.hasMember("batch_id")) {
            System.out.println("Missing batch_id in response.");
            return;
        }

        // Allocate one batch per independently cancelable appointment.
        System.out.println("Save this batch ID with the appointment: " + batchId);
        DateTime sendTime = new DateTime();
        sendTime.setFromCurrentSystemTime();
        sendTime.addSeconds(3600);
        JsonObject message = new JsonObject();
        message.updateString("batch_id", batchId);
        message.updateInt("send_at", (int) sendTime.getAsUnixTime(false));
        message.updateString("from.email", "sender@example.com");
        message.updateString("personalizations[0].to[0].email", "recipient@example.com");
        message.updateString("subject", "Appointment reminder");
        message.updateString("content[0].type", "text/plain");
        message.updateString("content[0].value", "Your appointment is tomorrow at 9:30 AM Central.");

        try {
            http.httpJson("POST", "https://api.sendgrid.com/v3/mail/send", message, "application/json", resp);
        } catch (ChilkatException e) {
            if (resp.getStatusCode() == 0) {
                System.out.println(e.getLastErrorText());
            } else {
                System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
            }

            return;
        }

        if (resp.getStatusCode() != 202) {
            System.out.println("HTTP " + resp.getStatusCode() + ": " + resp.getBodyStr());
            return;
        }

        System.out.println("Reminder scheduled for " + sendTime.getAsTimestamp(false));
    }
}