Constant Contact V3: LIVE SEND - Schedule a newsletter for a named list
See more Constant Contact Examples
LIVE SEND. Scheduling this campaign delivers email to eligible contacts on the selected list at the specified time. Use the reviewer test to check the email first. Select the intended newsletter and list in the same account. The example accepts a primary draft, replaces its entire audience with one active email list, and schedules it. List lookup requires contact_data. Editing and scheduling require campaign_data and account privileges to edit and send.
Enter a future ISO 8601 timestamp with a UTC offset or Z. To cancel before delivery starts, keep the same activity selection, change action to unschedule, and run again. Cancellation removes the schedule and keeps the audience assignment. If the scheduling response is lost, check the activity’s schedule before retrying.
Authorize the application using the same account and credential store.
Constant Contact developer guide
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Secrets;
public class ChilkatExample {
public static void main(String[] args) {
boolean success = false;
// LIVE SEND: Scheduling this activity delivers email to the selected contact list.
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Secrets vault = new Secrets();
vault.setLocation("local_manager");
vault.setAutoSplit(true);
JsonObject entry = new JsonObject();
entry.updateString("appName", "ConstantContactV3");
entry.updateString("service", "OAuth2");
entry.updateString("username", "my-account");
JsonObject tokens = new JsonObject();
try {
vault.getSecretJson(entry, tokens);
} catch (ChilkatException e) {
System.out.println("Run the desktop authorization example using this account and store.");
return;
}
if (tokens.jsonTypeOf("access_token") != 1) {
System.out.println("The saved authorization has no access token.");
return;
}
Http http = new Http();
http.setRequireSslCertVerify(true);
http.setAuthToken(tokens.stringOf("access_token"));
HttpResponse resp = new HttpResponse();
// Use the selection file from the draft or campaign lookup example for this account.
JsonObject selection = new JsonObject();
try {
selection.loadFile("newsletter-selection.json");
success = true;
} catch (ChilkatException e) {
success = false;
}
String activityId = selection.stringOf("campaign_activity_id");
if ((!success) || ("".equals(activityId))) {
System.out.println("Select a campaign and save its primary activity ID first.");
return;
}
http.setUrlVar("activity", activityId);
String action = "schedule";
if ("unschedule".equals(action)) {
try {
http.httpNoBody("DELETE", "https://api.cc.email/v3/emails/activities/{$activity}/schedules", resp);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) && (resp.getStatusCode() <= 0)) {
System.out.println("Remove schedule: HTTP request failed.");
return;
}
if ((!success) || (resp.getStatusCode() != 204)) {
System.out.println("Remove schedule: HTTP " + resp.getStatusCode() + " " + resp.getBodyStr());
return;
}
System.out.println("Schedule removed for activity " + activityId);
return;
}
if (!"schedule".equals(action)) {
System.out.println("Choose schedule or unschedule.");
return;
}
// Replace the placeholder with the intended future delivery time, e.g. 2030-05-15T09:00:00-05:00.
String deliveryTime = "ENTER_FUTURE_TIMESTAMP_WITH_OFFSET";
DateTime delivery = new DateTime();
DateTime now = new DateTime();
now.setFromCurrentSystemTime();
try {
delivery.setFromTimestamp(deliveryTime);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) || (delivery.diffSeconds(now) <= 0)) {
System.out.println("Enter a valid future delivery time with a UTC offset or Z.");
return;
}
// Use the full display name. AddParam encodes spaces and punctuation.
String listName = "Workshop announcements";
HttpRequest query = new HttpRequest();
query.setHttpVerb("GET");
query.addParam("name", listName);
query.addParam("status", "active");
query.addParam("include_membership_count", "active");
try {
http.httpReq("https://api.cc.email/v3/contact_lists", query, resp);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) && (resp.getStatusCode() <= 0)) {
System.out.println("Look up list: HTTP request failed.");
return;
}
if ((!success) || (resp.getStatusCode() != 200)) {
System.out.println("Look up list: HTTP " + resp.getStatusCode() + " " + resp.getBodyStr());
return;
}
JsonObject matches = new JsonObject();
try {
matches.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Expected a JSON object in the response.");
return;
}
if (matches.sizeOfArray("lists") == 0) {
System.out.println("No active email list named " + listName);
return;
}
if ((matches.sizeOfArray("lists") != 1) || (!matches.stringOfEquals("lists[0].name", listName, true))) {
System.out.println("Expected one exact list name.");
return;
}
String listId = matches.stringOf("lists[0].list_id");
if ("".equals(listId)) {
System.out.println("Missing list ID.");
return;
}
System.out.println(listName + " | List ID: " + listId + " | Mailable contacts: " + matches.intOf("lists[0].membership_count"));
// PUT replaces the activity. Read it first to preserve the subject, sender and other settings.
// Constant Contact retains omitted subresources such as html_content and the physical footer.
try {
http.httpNoBody("GET", "https://api.cc.email/v3/emails/activities/{$activity}", resp);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) && (resp.getStatusCode() <= 0)) {
System.out.println("Read draft: HTTP request failed.");
return;
}
if ((!success) || (resp.getStatusCode() != 200)) {
System.out.println("Read draft: HTTP " + resp.getStatusCode() + " " + resp.getBodyStr());
return;
}
JsonObject draft = new JsonObject();
try {
draft.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Expected a JSON object in the response.");
return;
}
if ((!draft.stringOfEquals("role", "primary_email", true)) || (!draft.stringOfEquals("current_status", "DRAFT", true))) {
System.out.println("Select a primary activity in DRAFT status.");
return;
}
System.out.println("LIVE SEND: " + draft.stringOf("subject") + " | Activity: " + activityId + " | List: " + listName + " | Delivery: " + deliveryTime);
draft.updateNewArray("contact_list_ids");
draft.updateString("contact_list_ids[0]", listId);
draft.updateNewArray("segment_ids");
try {
http.httpJson("PUT", "https://api.cc.email/v3/emails/activities/{$activity}", draft, "application/json", resp);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) && (resp.getStatusCode() <= 0)) {
System.out.println("Assign audience: HTTP request failed.");
return;
}
if ((!success) || (resp.getStatusCode() != 200)) {
System.out.println("Assign audience: HTTP " + resp.getStatusCode() + " " + resp.getBodyStr());
return;
}
JsonObject updated = new JsonObject();
try {
updated.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Expected a JSON object in the response.");
return;
}
if ((updated.sizeOfArray("contact_list_ids") != 1) || ((!updated.stringOfEquals("contact_list_ids[0]", listId, true)) || (updated.sizeOfArray("segment_ids") > 0))) {
System.out.println("The saved audience did not match the requested list. Scheduling stopped.");
return;
}
JsonObject schedule = new JsonObject();
schedule.updateString("scheduled_date", delivery.getAsTimestamp(false));
try {
http.httpJson("POST", "https://api.cc.email/v3/emails/activities/{$activity}/schedules", schedule, "application/json", resp);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) || ((resp.getStatusCode() < 200) || (resp.getStatusCode() >= 300))) {
System.out.println("Schedule not confirmed. HTTP " + resp.getStatusCode());
System.out.println("The audience was saved. Check the schedule before retrying.");
return;
}
System.out.println("Live delivery scheduled for " + schedule.stringOf("scheduled_date") + " to " + listName);
}
}