Sample code for 30+ languages & platforms
C

Amazon SES v2: Look up delivery history for a message ID

See more Amazon SES Examples

Enable Virtual Deliverability Manager for the sending account. Use the SES MessageId from a recent eligible send to a real test mailbox in the same Region. Mailbox simulator sends are excluded from VDM. Events are grouped by destination. Timestamps in the REST response are Unix epoch seconds.

GetMessageInsights · VDM dashboard

Chilkat C Downloads

C
#include <C_CkStringBuilder.h>
#include <C_CkAuthAws.h>
#include <C_CkSocket.h>
#include <C_CkRest.h>
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    BOOL success;
    const char *messageId;
    HCkStringBuilder path;
    const char *region;
    HCkStringBuilder host;
    HCkAuthAws signer;
    HCkSocket connection;
    HCkRest rest;
    const char *sessionToken;
    const char *responseText;
    HCkJsonObject response;
    int i;
    int recipients;
    int j;
    int count;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = FALSE;

    messageId = "YOUR_SES_MESSAGE_ID";
    path = CkStringBuilder_Create();
    CkStringBuilder_Append(path,"/v2/email/insights/");
    CkStringBuilder_Append(path,messageId);
    CkStringBuilder_Append(path,"/");
    //  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
    region = "us-west-2";
    host = CkStringBuilder_Create();
    CkStringBuilder_Append(host,"email.");
    CkStringBuilder_Append(host,region);
    CkStringBuilder_Append(host,".amazonaws.com");

    signer = CkAuthAws_Create();
    //  Supply the credentials from your application's credential provider.
    CkAuthAws_putAccessKey(signer,"AWS_ACCESS_KEY_ID");
    CkAuthAws_putSecretKey(signer,"AWS_SECRET_ACCESS_KEY");
    CkAuthAws_putRegion(signer,region);
    CkAuthAws_putServiceName(signer,"ses");

    connection = CkSocket_Create();
    CkSocket_putRequireSslCertVerify(connection,TRUE);
    success = CkSocket_Connect(connection,CkStringBuilder_getAsString(host),443,TRUE,10000);
    if (success != TRUE) {
        printf("%s\n",CkSocket_lastErrorText(connection));
        CkStringBuilder_Dispose(path);
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        return;
    }

    rest = CkRest_Create();
    success = CkRest_UseConnection(rest,connection,FALSE);
    if (success != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkStringBuilder_Dispose(path);
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        return;
    }

    success = CkRest_SetAuthAws(rest,signer);
    if (success != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkStringBuilder_Dispose(path);
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        return;
    }

    //  For temporary AWS credentials, supply the session token from the same provider.
    sessionToken = "";
    if (sessionToken != "") {
        CkRest_AddQueryParam(rest,"X-Amz-Security-Token",sessionToken);
    }

    responseText = CkRest_fullRequestNoBody(rest,"GET",CkStringBuilder_getAsString(path));
    if (CkRest_getLastMethodSuccess(rest) != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkStringBuilder_Dispose(path);
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        return;
    }

    if (CkRest_getResponseStatusCode(rest) == 404) {
        printf("No insights found. Check the Region, message ID, and VDM coverage.\n");
        CkStringBuilder_Dispose(path);
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        return;
    }

    if (CkRest_getResponseStatusCode(rest) != 200) {
        printf("SES returned HTTP %d %s\n",CkRest_getResponseStatusCode(rest),responseText);
        CkStringBuilder_Dispose(path);
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        return;
    }

    response = CkJsonObject_Create();
    success = CkJsonObject_Load(response,responseText);
    if (success != TRUE) {
        printf("%s\n",CkJsonObject_lastErrorText(response));
        CkStringBuilder_Dispose(path);
        CkStringBuilder_Dispose(host);
        CkAuthAws_Dispose(signer);
        CkSocket_Dispose(connection);
        CkRest_Dispose(rest);
        CkJsonObject_Dispose(response);
        return;
    }

    i = 0;
    recipients = CkJsonObject_SizeOfArray(response,"Insights");
    if (recipients < 1) {
        printf("No recipient history is available yet.\n");
    }

    while ((i < recipients)) {
        CkJsonObject_putI(response,i);
        printf("%s\n",CkJsonObject_stringOf(response,"Insights[i].Destination"));
        j = 0;
        count = CkJsonObject_SizeOfArray(response,"Insights[i].Events");
        while ((j < count)) {
            CkJsonObject_putJ(response,j);
            printf("%s %s\n",CkJsonObject_stringOf(response,"Insights[i].Events[j].Timestamp"),CkJsonObject_stringOf(response,"Insights[i].Events[j].Type"));
            if (CkJsonObject_HasMember(response,"Insights[i].Events[j].Details.Bounce.DiagnosticCode") == TRUE) {
                printf("%s\n",CkJsonObject_stringOf(response,"Insights[i].Events[j].Details.Bounce.DiagnosticCode"));
            }

            if (CkJsonObject_HasMember(response,"Insights[i].Events[j].Details.Complaint.ComplaintFeedbackType") == TRUE) {
                printf("%s\n",CkJsonObject_stringOf(response,"Insights[i].Events[j].Details.Complaint.ComplaintFeedbackType"));
            }

            j = j + 1;
        }

        i = i + 1;
    }



    CkStringBuilder_Dispose(path);
    CkStringBuilder_Dispose(host);
    CkAuthAws_Dispose(signer);
    CkSocket_Dispose(connection);
    CkRest_Dispose(rest);
    CkJsonObject_Dispose(response);

    }