Sample code for 30+ languages & platforms
Unicode C

Amazon SES v2: Send HTML with a plain text alternative

See more Amazon SES Examples

Send a single report notification with an HTML body and a plain text alternative. Each part conveys the same information so the recipient can read it with either kind of mail client. Use a verified sender in the sending Region and grant ses:SendEmail.

SendEmail

Chilkat Unicode C Downloads

Unicode C
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkSocketW.h>
#include <C_CkRestW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkJsonObjectW request;
    const wchar_t *region;
    HCkStringBuilderW host;
    HCkAuthAwsW signer;
    HCkSocketW connection;
    HCkRestW rest;
    const wchar_t *sessionToken;
    const wchar_t *responseText;
    HCkJsonObjectW result;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    success = FALSE;

    //  Replace the From address with your verified sender. The destination is the SES mailbox simulator.
    request = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(request,L"FromEmailAddress",L"sender+reports@example.com");
    CkJsonObjectW_UpdateString(request,L"Destination.ToAddresses[0]",L"success@simulator.amazonses.com");
    CkJsonObjectW_UpdateString(request,L"Content.Simple.Subject.Data",L"Weekly report: 12 orders shipped");
    CkJsonObjectW_UpdateString(request,L"Content.Simple.Subject.Charset",L"UTF-8");
    //  SES builds a multipart/alternative message from the two body representations.
    CkJsonObjectW_UpdateString(request,L"Content.Simple.Body.Text.Charset",L"UTF-8");
    CkJsonObjectW_UpdateString(request,L"Content.Simple.Body.Text.Data",L"Weekly report: 12 orders shipped.");
    CkJsonObjectW_UpdateString(request,L"Content.Simple.Body.Html.Charset",L"UTF-8");
    CkJsonObjectW_UpdateString(request,L"Content.Simple.Body.Html.Data",L"<h1>Weekly report</h1><p><strong>12 orders</strong> shipped.</p>");

    //  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
    region = L"us-west-2";
    host = CkStringBuilderW_Create();
    CkStringBuilderW_Append(host,L"email.");
    CkStringBuilderW_Append(host,region);
    CkStringBuilderW_Append(host,L".amazonaws.com");

    signer = CkAuthAwsW_Create();
    //  Supply the credentials from your application's credential provider.
    CkAuthAwsW_putAccessKey(signer,L"AWS_ACCESS_KEY_ID");
    CkAuthAwsW_putSecretKey(signer,L"AWS_SECRET_ACCESS_KEY");
    CkAuthAwsW_putRegion(signer,region);
    CkAuthAwsW_putServiceName(signer,L"ses");

    connection = CkSocketW_Create();
    CkSocketW_putRequireSslCertVerify(connection,TRUE);
    success = CkSocketW_Connect(connection,CkStringBuilderW_getAsString(host),443,TRUE,10000);
    if (success != TRUE) {
        wprintf(L"%s\n",CkSocketW_lastErrorText(connection));
        CkJsonObjectW_Dispose(request);
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        return;
    }

    rest = CkRestW_Create();
    success = CkRestW_UseConnection(rest,connection,FALSE);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkJsonObjectW_Dispose(request);
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        return;
    }

    success = CkRestW_SetAuthAws(rest,signer);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkJsonObjectW_Dispose(request);
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        return;
    }

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

    CkRestW_AddHeader(rest,L"Content-Type",L"application/json");
    responseText = CkRestW_fullRequestString(rest,L"POST",L"/v2/email/outbound-emails",CkJsonObjectW_emit(request));
    if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkJsonObjectW_Dispose(request);
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        return;
    }

    if (CkRestW_getResponseStatusCode(rest) != 200) {
        wprintf(L"SES returned HTTP %d %s\n",CkRestW_getResponseStatusCode(rest),responseText);
        CkJsonObjectW_Dispose(request);
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        return;
    }

    result = CkJsonObjectW_Create();
    success = CkJsonObjectW_Load(result,responseText);
    if (success != TRUE) {
        wprintf(L"%s\n",CkJsonObjectW_lastErrorText(result));
        CkJsonObjectW_Dispose(request);
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        CkJsonObjectW_Dispose(result);
        return;
    }

    if (CkJsonObjectW_HasMember(result,L"MessageId") != TRUE) {
        wprintf(L"SES returned no message ID.\n");
        CkJsonObjectW_Dispose(request);
        CkStringBuilderW_Dispose(host);
        CkAuthAwsW_Dispose(signer);
        CkSocketW_Dispose(connection);
        CkRestW_Dispose(rest);
        CkJsonObjectW_Dispose(result);
        return;
    }

    wprintf(L"SES accepted the message. Message ID: %s\n",CkJsonObjectW_stringOf(result,L"MessageId"));


    CkJsonObjectW_Dispose(request);
    CkStringBuilderW_Dispose(host);
    CkAuthAwsW_Dispose(signer);
    CkSocketW_Dispose(connection);
    CkRestW_Dispose(rest);
    CkJsonObjectW_Dispose(result);

    }