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

Constant Contact V3: Send a test email to reviewers

See more Constant Contact Examples

Read the primary activity ID from newsletter-selection.json. Replace the two reviewer addresses and send a preview with a personal note. Only the specified reviewers receive it. The request leaves the campaign schedule and contact lists alone.

Use up to five addresses per request. Each recipient counts toward the 50 daily test emails. Contact personalization and custom field values are not expanded in test messages. Success is HTTP 204 with an empty body.

Authorize the application using the same account and credential store.

Constant Contact developer guide

Chilkat Java Downloads

Java
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;

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();

        // 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);
        JsonObject testMail = new JsonObject();
        testMail.updateString("email_addresses[0]", "editor@example.com");
        testMail.updateString("email_addresses[1]", "reviewer@example.com");
        testMail.updateString("personal_message", "Please check the workshop dates and the registration link.");
        try {
            http.httpJson("POST", "https://api.cc.email/v3/emails/activities/{$activity}/tests", testMail, "application/json", resp);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if (resp.getStatusCode() == 429) {
            System.out.println("HTTP 429: Test-send limit reached. Each reviewer counts toward the daily allowance.");
            return;
        }

        if ((!success) && (resp.getStatusCode() <= 0)) {
            System.out.println("Send reviewer test: HTTP request failed.");
            return;
        }

        if ((!success) || (resp.getStatusCode() != 204)) {
            System.out.println("Send reviewer test: HTTP " + resp.getStatusCode() + " " + resp.getBodyStr());
            return;
        }

        System.out.println("Test email accepted for the two reviewer addresses.");
    }
}