Java Requires Chilkat v11.0.0+
Java
Constant Contact V3: Create a newsletter draft from an HTML file
See more Constant Contact Examples
Save your newsletter as UTF-8 in newsletter.html. Put the tracking tag inside the body, for example <body><h1>Workshop news</h1>[[trackingImage]]</body>. Supply a unique campaign name and confirmed sender and reply-to addresses for your account. Constant Contact uses the account’s physical mailing address in the footer by default.
This creates a custom-code draft. The example writes the campaign and primary activity IDs to newsletter-selection.json for the test and scheduling examples. If the response is lost after creation, look up the campaign by name before creating another draft.
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.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Secrets;
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.
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();
StringBuilder html = new StringBuilder();
try {
html.loadFile("newsletter.html", "utf-8");
} catch (ChilkatException e) {
System.out.println("Could not read newsletter.html.");
return;
}
if (!html.contains("[[trackingImage]]", true)) {
System.out.println("Add [[trackingImage]] inside the HTML body.");
return;
}
JsonObject draft = new JsonObject();
draft.updateString("name", "Workshop newsletter");
draft.updateInt("email_campaign_activities[0].format_type", 5);
draft.updateString("email_campaign_activities[0].from_name", "Workshop team");
draft.updateString("email_campaign_activities[0].from_email", "news@example.com");
draft.updateString("email_campaign_activities[0].reply_to_email", "workshops@example.com");
draft.updateString("email_campaign_activities[0].subject", "Upcoming workshops");
draft.updateString("email_campaign_activities[0].html_content", html.getAsString());
try {
http.httpJson("POST", "https://api.cc.email/v3/emails", draft, "application/json", resp);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) && (resp.getStatusCode() <= 0)) {
System.out.println("Create draft: HTTP request failed.");
return;
}
if ((!success) || (resp.getStatusCode() != 201)) {
System.out.println("Create draft: HTTP " + resp.getStatusCode() + " " + resp.getBodyStr());
return;
}
JsonObject campaign = new JsonObject();
try {
campaign.load(resp.getBodyStr());
} catch (ChilkatException e) {
System.out.println("Expected a JSON object in the response.");
return;
}
// The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
JsonObject primary = new JsonObject();
try {
campaign.findRecord2("campaign_activities", "role", "primary_email", true, primary);
success = true;
} catch (ChilkatException e) {
success = false;
}
if ((!success) || (primary.jsonTypeOf("campaign_activity_id") != 1)) {
System.out.println("No primary email activity ID in the response.");
return;
}
String selectedCampaign = campaign.stringOf("campaign_id");
String selectedActivity = primary.stringOf("campaign_activity_id");
if (("".equals(selectedCampaign)) || ("".equals(selectedActivity))) {
System.out.println("Missing campaign or activity ID.");
return;
}
System.out.println("Campaign ID: " + selectedCampaign);
System.out.println("Primary activity ID: " + selectedActivity);
JsonObject selection = new JsonObject();
selection.updateString("name", campaign.stringOf("name"));
selection.updateString("campaign_id", selectedCampaign);
selection.updateString("campaign_activity_id", selectedActivity);
try {
selection.writeFile("newsletter-selection.json");
} catch (ChilkatException e) {
System.out.println("Could not save newsletter-selection.json. Keep the IDs printed above.");
return;
}
System.out.println("Saved newsletter-selection.json.");
}
}