Java Requires Chilkat v11.0.0+
Java
SendGrid: Create a newsletter draft, then schedule the reviewed draft
See more SendGrid Examples
Prepare a Marketing Campaigns design with a subject, content, and an unsubscribe module. Supply the design and list UUIDs, a verified marketing sender ID, and the unsubscribe group ID. The first run creates only a draft. After reviewing the audience and content, run the scheduling branch with that same ID. Single Sends use an ISO 8601 UTC string, unlike Mail Send’s numeric Unix timestamp.
Create a Single Send · Schedule a Single Send
Chilkat Java Downloads
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();
// Leave this empty to create a draft. To schedule, supply the ID of an already reviewed draft.
String reviewedDraftId = "";
if ("".equals(reviewedDraftId)) {
JsonObject draft = new JsonObject();
draft.updateString("name", "November newsletter");
draft.updateString("send_to.list_ids[0]", "YOUR_LIST_UUID");
draft.updateInt("email_config.sender_id", 12345);
draft.updateInt("email_config.suppression_group_id", 67890);
draft.updateString("email_config.design_id", "YOUR_NEWSLETTER_DESIGN_UUID");
try {
http.httpJson("POST", "https://api.sendgrid.com/v3/marketing/singlesends", draft, "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;
}
JsonObject created = new JsonObject();
try {
created.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Unexpected response: expected JSON.");
return;
}
String draftId = created.stringOf("id");
if (!created.hasMember("id")) {
System.out.println("Missing id in response.");
return;
}
System.out.println("Draft created: " + draftId + ". Review it in Marketing Campaigns before scheduling.");
return;
}
// The example selects tomorrow at the current UTC time. Use your approved send time.
DateTime releaseTime = new DateTime();
releaseTime.setFromCurrentSystemTime();
releaseTime.addDays(1);
JsonObject schedule = new JsonObject();
schedule.updateString("send_at", releaseTime.getAsTimestamp(false));
http.setUrlVar("draft", reviewedDraftId);
try {
http.httpJson("PUT", "https://api.sendgrid.com/v3/marketing/singlesends/{$draft}/schedule", schedule, "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;
}
JsonObject scheduled = new JsonObject();
try {
scheduled.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Unexpected response: expected JSON.");
return;
}
System.out.println(scheduled.stringOf("status") + ": " + scheduled.stringOf("send_at"));
}
}