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

Amazon SES v2: Send a raw MIME email with a PDF attachment

See more Amazon SES Examples

Place invoice.pdf in the working directory. Chilkat builds the MIME message and gives the attachment the name invoice-1042.pdf. The v2 SendEmail request carries those bytes in Content.Raw.Data. For a blind copy, supply the address in Destination.BccAddresses and keep it out of the MIME headers.

Use a verified sender and grant ses:SendEmail for this v2 operation. The v2 message size limit is 40 MB including attachments after MIME encoding. For a straightforward attachment that needs no custom MIME layout, v2 also supports Content.Simple.Attachments.

Sending raw email · SES attachments · Sending quotas

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkBinDataW.h>
#include <CkEmailW.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;

    //  Read the PDF before making a network request.
    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;
    }

    const wchar_t *sender = L"sender+reports@example.com";
    const wchar_t *recipient = L"success@simulator.amazonses.com";
    CkEmailW email;
    email.put_From(sender);
    email.put_Subject(L"Invoice 1042");
    email.put_Charset(L"utf-8");
    success = email.AddTo(L"",recipient);
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    email.AddPlainTextAlternativeBody(L"Invoice 1042 is attached.");
    email.AddHtmlAlternativeBody(L"<p>Invoice <strong>1042</strong> is attached.</p>");
    success = email.AddAttachmentBd(L"invoice-1042.pdf",pdf,L"application/pdf");
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    //  Get the exact MIME bytes, including the attachment's transfer encoding.
    CkBinDataW mime;
    success = email.GetMimeBd(mime);
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    //  Base64 here transports the entire MIME message as a blob in the JSON request.
    CkJsonObjectW request;
    request.UpdateString(L"FromEmailAddress",sender);
    request.UpdateString(L"Destination.ToAddresses[0]",recipient);
    request.UpdateString(L"Content.Raw.Data",mime.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 result;
    success = result.Load(responseText);
    if (success != true) {
        wprintf(L"%s\n",result.lastErrorText());
        return;
    }

    if (result.HasMember(L"MessageId") != true) {
        wprintf(L"SES returned no message ID.\n");
        return;
    }

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