Sample code for 30+ languages & platforms
Unicode C 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 Unicode C Downloads

Unicode C
#include <C_CkSecretsW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSecretsW vault;
    HCkJsonObjectW entry;
    HCkJsonObjectW tokens;
    BOOL loaded;
    HCkHttpW http;
    HCkHttpResponseW resp;
    HCkJsonObjectW selection;
    const wchar_t *activityId;
    HCkJsonObjectW testMail;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    vault = CkSecretsW_Create();
    CkSecretsW_putLocation(vault,L"local_manager");
    CkSecretsW_putAutoSplit(vault,TRUE);
    entry = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(entry,L"appName",L"ConstantContactV3");
    CkJsonObjectW_UpdateString(entry,L"service",L"OAuth2");
    CkJsonObjectW_UpdateString(entry,L"username",L"my-account");
    tokens = CkJsonObjectW_Create();
    loaded = CkSecretsW_GetSecretJson(vault,entry,tokens);
    if (loaded != TRUE) {
        wprintf(L"Run the desktop authorization example using this account and store.\n");
        CkSecretsW_Dispose(vault);
        CkJsonObjectW_Dispose(entry);
        CkJsonObjectW_Dispose(tokens);
        return;
    }

    if (CkJsonObjectW_JsonTypeOf(tokens,L"access_token") != 1) {
        wprintf(L"The saved authorization has no access token.\n");
        CkSecretsW_Dispose(vault);
        CkJsonObjectW_Dispose(entry);
        CkJsonObjectW_Dispose(tokens);
        return;
    }

    http = CkHttpW_Create();
    CkHttpW_putRequireSslCertVerify(http,TRUE);
    CkHttpW_putAuthToken(http,CkJsonObjectW_stringOf(tokens,L"access_token"));
    resp = CkHttpResponseW_Create();

    //  Use the selection file from the draft or campaign lookup example for this account.
    selection = CkJsonObjectW_Create();
    success = CkJsonObjectW_LoadFile(selection,L"newsletter-selection.json");
    activityId = CkJsonObjectW_stringOf(selection,L"campaign_activity_id");
    if ((success != TRUE) || (activityId == L"")) {
        wprintf(L"Select a campaign and save its primary activity ID first.\n");
        CkSecretsW_Dispose(vault);
        CkJsonObjectW_Dispose(entry);
        CkJsonObjectW_Dispose(tokens);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(selection);
        return;
    }

    CkHttpW_SetUrlVar(http,L"activity",activityId);
    testMail = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(testMail,L"email_addresses[0]",L"editor@example.com");
    CkJsonObjectW_UpdateString(testMail,L"email_addresses[1]",L"reviewer@example.com");
    CkJsonObjectW_UpdateString(testMail,L"personal_message",L"Please check the workshop dates and the registration link.");
    success = CkHttpW_HttpJson(http,L"POST",L"https://api.cc.email/v3/emails/activities/{$activity}/tests",testMail,L"application/json",resp);
    if (CkHttpResponseW_getStatusCode(resp) == 429) {
        wprintf(L"HTTP 429: Test-send limit reached. Each reviewer counts toward the daily allowance.\n");
        CkSecretsW_Dispose(vault);
        CkJsonObjectW_Dispose(entry);
        CkJsonObjectW_Dispose(tokens);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(selection);
        CkJsonObjectW_Dispose(testMail);
        return;
    }

    if ((success != TRUE) && (CkHttpResponseW_getStatusCode(resp) <= 0)) {
        wprintf(L"Send reviewer test: HTTP request failed.\n");
        CkSecretsW_Dispose(vault);
        CkJsonObjectW_Dispose(entry);
        CkJsonObjectW_Dispose(tokens);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(selection);
        CkJsonObjectW_Dispose(testMail);
        return;
    }

    if ((success != TRUE) || (CkHttpResponseW_getStatusCode(resp) != 204)) {
        wprintf(L"Send reviewer test: HTTP %d %s\n",CkHttpResponseW_getStatusCode(resp),CkHttpResponseW_bodyStr(resp));
        CkSecretsW_Dispose(vault);
        CkJsonObjectW_Dispose(entry);
        CkJsonObjectW_Dispose(tokens);
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(selection);
        CkJsonObjectW_Dispose(testMail);
        return;
    }

    wprintf(L"Test email accepted for the two reviewer addresses.\n");


    CkSecretsW_Dispose(vault);
    CkJsonObjectW_Dispose(entry);
    CkJsonObjectW_Dispose(tokens);
    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);
    CkJsonObjectW_Dispose(selection);
    CkJsonObjectW_Dispose(testMail);

    }