Sample code for 30+ languages & platforms
Java

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 Java Downloads

Java
import com.chilkatsoft.chilkat2.AuthAws;
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Email;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.Socket;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

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

        success = false;

        // Read the PDF before making a network request.
        BinData pdf = new BinData();
        try {
            pdf.loadFile("invoice.pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        if (pdf.getNumBytes() == 0) {
            System.out.println("invoice.pdf is empty.");
            return;
        }

        String sender = "sender+reports@example.com";
        String recipient = "success@simulator.amazonses.com";
        Email email = new Email();
        email.setFrom(sender);
        email.setSubject("Invoice 1042");
        email.setCharset("utf-8");
        try {
            email.addTo("", recipient);

            email.addPlainTextAlternativeBody("Invoice 1042 is attached.");
            email.addHtmlAlternativeBody("<p>Invoice <strong>1042</strong> is attached.</p>");
            email.addAttachmentBd("invoice-1042.pdf", pdf, "application/pdf");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Get the exact MIME bytes, including the attachment's transfer encoding.
        BinData mime = new BinData();
        try {
            email.getMimeBd(mime);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // Base64 here transports the entire MIME message as a blob in the JSON request.
        JsonObject request = new JsonObject();
        request.updateString("FromEmailAddress", sender);
        request.updateString("Destination.ToAddresses[0]", recipient);
        request.updateString("Content.Raw.Data", mime.getEncoded("base64"));

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

        AuthAws signer = new AuthAws();
        // Supply the credentials from your application's credential provider.
        signer.setAccessKey("AWS_ACCESS_KEY_ID");
        signer.setSecretKey("AWS_SECRET_ACCESS_KEY");
        signer.setRegion(region);
        signer.setServiceName("ses");

        Socket connection = new Socket();
        connection.setRequireSslCertVerify(true);
        try {
            connection.connect(host.getAsString(), 443, true, 10000);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Rest rest = new Rest();
        try {
            rest.useConnection(connection, false);

            rest.setAuthAws(signer);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

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

        rest.addHeader("Content-Type", "application/json");
        String responseText;
        try {
            responseText = rest.fullRequestString("POST", "/v2/email/outbound-emails", request.emit());
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        if (rest.getResponseStatusCode() != 200) {
            System.out.println("SES returned HTTP " + rest.getResponseStatusCode() + " " + responseText);
            return;
        }

        JsonObject result = new JsonObject();
        try {
            result.load(responseText);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        if (!result.hasMember("MessageId")) {
            System.out.println("SES returned no message ID.");
            return;
        }

        System.out.println("SES accepted the message. Message ID: " + result.stringOf("MessageId"));
    }
}