Sample code for 30+ languages & platforms
Unicode C++

Amazon SES v2: Attach a PDF to a simple email

See more Amazon SES Examples

Place invoice.pdf in the working directory and use a verified sender. RawContent contains the Base64 representation of the file bytes for JSON transport. ContentTransferEncoding tells SES how to encode the PDF when it assembles the outgoing MIME message. For custom MIME structure, see Send a raw MIME email.

SES attachments · SendEmail

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkBinDataW.h>
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.h>
#include <CkAuthAwsW.h>
#include <CkSocketW.h>
#include <CkRestW.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkBinDataW pdf;
    success = pdf.LoadFile(L"invoice.pdf");
    if (success != true) {
        wprintf(L"%s\n",pdf.lastErrorText());
        return;
    }

    if (pdf.get_NumBytes() == 0) {
        wprintf(L"invoice.pdf is empty.\n");
        return;
    }

    CkJsonObjectW request;
    request.UpdateString(L"FromEmailAddress",L"sender@example.com");
    request.UpdateString(L"Destination.ToAddresses[0]",L"success@simulator.amazonses.com");
    request.UpdateString(L"Content.Simple.Subject.Data",L"Invoice 1042");
    request.UpdateString(L"Content.Simple.Body.Text.Data",L"Your invoice is attached.");
    request.UpdateString(L"Content.Simple.Attachments[0].FileName",L"invoice-1042.pdf");
    request.UpdateString(L"Content.Simple.Attachments[0].ContentType",L"application/pdf");
    request.UpdateString(L"Content.Simple.Attachments[0].ContentDisposition",L"ATTACHMENT");
    request.UpdateString(L"Content.Simple.Attachments[0].ContentTransferEncoding",L"BASE64");
    request.UpdateString(L"Content.Simple.Attachments[0].RawContent",pdf.getEncoded(L"base64"));
    //  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
    const wchar_t *region = L"us-west-2";
    CkStringBuilderW host;
    host.Append(L"email.");
    host.Append(region);
    host.Append(L".amazonaws.com");

    CkAuthAwsW signer;
    //  Supply the credentials from your application's credential provider.
    signer.put_AccessKey(L"AWS_ACCESS_KEY_ID");
    signer.put_SecretKey(L"AWS_SECRET_ACCESS_KEY");
    signer.put_Region(region);
    signer.put_ServiceName(L"ses");

    CkSocketW connection;
    connection.put_RequireSslCertVerify(true);
    success = connection.Connect(host.getAsString(),443,true,10000);
    if (success != true) {
        wprintf(L"%s\n",connection.lastErrorText());
        return;
    }

    CkRestW rest;
    success = rest.UseConnection(connection,false);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    success = rest.SetAuthAws(signer);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

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

    rest.AddHeader(L"Content-Type",L"application/json");
    const wchar_t *responseText = rest.fullRequestString(L"POST",L"/v2/email/outbound-emails",request.emit());
    if (rest.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    if (rest.get_ResponseStatusCode() != 200) {
        wprintf(L"SES returned HTTP %d %s\n",rest.get_ResponseStatusCode(),responseText);
        return;
    }

    CkJsonObjectW response;
    success = response.Load(responseText);
    if (success != true) {
        wprintf(L"%s\n",response.lastErrorText());
        return;
    }

    wprintf(L"SES accepted the message: %s\n",response.stringOf(L"MessageId"));
    }