Sample code for 30+ languages & platforms
Java

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

Java
import com.chilkatsoft.chilkat2.AuthAws;
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
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;

        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;
        }

        JsonObject request = new JsonObject();
        request.updateString("FromEmailAddress", "sender@example.com");
        request.updateString("Destination.ToAddresses[0]", "success@simulator.amazonses.com");
        request.updateString("Content.Simple.Subject.Data", "Invoice 1042");
        request.updateString("Content.Simple.Body.Text.Data", "Your invoice is attached.");
        request.updateString("Content.Simple.Attachments[0].FileName", "invoice-1042.pdf");
        request.updateString("Content.Simple.Attachments[0].ContentType", "application/pdf");
        request.updateString("Content.Simple.Attachments[0].ContentDisposition", "ATTACHMENT");
        request.updateString("Content.Simple.Attachments[0].ContentTransferEncoding", "BASE64");
        request.updateString("Content.Simple.Attachments[0].RawContent", pdf.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 response = new JsonObject();
        try {
            response.load(responseText);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

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