Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
pdf: HCkBinData;
sender: PWideChar;
recipient: PWideChar;
email: HCkEmail;
mime: HCkBinData;
request: HCkJsonObject;
region: PWideChar;
host: HCkStringBuilder;
signer: HCkAuthAws;
connection: HCkSocket;
rest: HCkRest;
sessionToken: PWideChar;
responseText: PWideChar;
result: HCkJsonObject;

begin
success := False;

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

success := False;

//  Read the PDF before making a network request.
pdf := CkBinData_Create();
success := CkBinData_LoadFile(pdf,'invoice.pdf');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkBinData__lastErrorText(pdf));
    Exit;
  end;
if (CkBinData_getNumBytes(pdf) = 0) then
  begin
    Memo1.Lines.Add('invoice.pdf is empty.');
    Exit;
  end;
sender := 'sender+reports@example.com';
recipient := 'success@simulator.amazonses.com';
email := CkEmail_Create();
CkEmail_putFrom(email,sender);
CkEmail_putSubject(email,'Invoice 1042');
CkEmail_putCharset(email,'utf-8');
success := CkEmail_AddTo(email,'',recipient);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
CkEmail_AddPlainTextAlternativeBody(email,'Invoice 1042 is attached.');
CkEmail_AddHtmlAlternativeBody(email,'<p>Invoice <strong>1042</strong> is attached.</p>');
success := CkEmail_AddAttachmentBd(email,'invoice-1042.pdf',pdf,'application/pdf');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
//  Get the exact MIME bytes, including the attachment's transfer encoding.
mime := CkBinData_Create();
success := CkEmail_GetMimeBd(email,mime);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkEmail__lastErrorText(email));
    Exit;
  end;
//  Base64 here transports the entire MIME message as a blob in the JSON request.
request := CkJsonObject_Create();
CkJsonObject_UpdateString(request,'FromEmailAddress',sender);
CkJsonObject_UpdateString(request,'Destination.ToAddresses[0]',recipient);
CkJsonObject_UpdateString(request,'Content.Raw.Data',CkBinData__getEncoded(mime,'base64'));

//  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) then
  begin
    Memo1.Lines.Add(CkSocket__lastErrorText(connection));
    Exit;
  end;

rest := CkRest_Create();
success := CkRest_UseConnection(rest,connection,False);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;
success := CkRest_SetAuthAws(rest,signer);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;
//  For temporary AWS credentials, supply the session token from the same provider.
sessionToken := '';
if (sessionToken <> '') then
  begin
    CkRest_AddQueryParam(rest,'X-Amz-Security-Token',sessionToken);
  end;

CkRest_AddHeader(rest,'Content-Type','application/json');
responseText := CkRest__fullRequestString(rest,'POST','/v2/email/outbound-emails',CkJsonObject__emit(request));
if (CkRest_getLastMethodSuccess(rest) <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;
if (CkRest_getResponseStatusCode(rest) <> 200) then
  begin
    Memo1.Lines.Add('SES returned HTTP ' + IntToStr(CkRest_getResponseStatusCode(rest)) + ' ' + responseText);
    Exit;
  end;
result := CkJsonObject_Create();
success := CkJsonObject_Load(result,responseText);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkJsonObject__lastErrorText(result));
    Exit;
  end;
if (CkJsonObject_HasMember(result,'MessageId') <> True) then
  begin
    Memo1.Lines.Add('SES returned no message ID.');
    Exit;
  end;
Memo1.Lines.Add('SES accepted the message. Message ID: ' + CkJsonObject__stringOf(result,'MessageId'));

CkBinData_Dispose(pdf);
CkEmail_Dispose(email);
CkBinData_Dispose(mime);
CkJsonObject_Dispose(request);
CkStringBuilder_Dispose(host);
CkAuthAws_Dispose(signer);
CkSocket_Dispose(connection);
CkRest_Dispose(rest);
CkJsonObject_Dispose(result);