Sample code for 30+ languages & platforms
Java

Amazon SES v2: Create and preview a receipt template

See more Amazon SES Examples

Create the regional OrderReceipt template once. Set createTemplate to false to preview it with sample order data. To exercise the missing-field case, omit orderNumber from the preview data and inspect the rendering error. A successful preview returns MIME content without sending a message.

CreateEmailTemplate · TestRenderEmailTemplate

Chilkat Java Downloads

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

        boolean createTemplate = true;
        // 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);
        }

        if (createTemplate) {
            JsonObject request = new JsonObject();
            request.updateString("TemplateName", "OrderReceipt");
            request.updateString("TemplateContent.Subject", "Receipt for {{orderNumber}}");
            request.updateString("TemplateContent.Text", "Thank you, {{firstName}}. Order {{orderNumber}}: {{#each items}}{{quantity}} x {{name}}. {{/each}}");
            request.updateString("TemplateContent.Html", "<p>Thank you, {{firstName}}.</p><ul>{{#each items}}<li>{{quantity}} x {{name}}</li>{{/each}}</ul>");

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

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

            System.out.println("Template created. Set createTemplate to false to preview it.");
            return;
        }

        JsonObject order = new JsonObject();
        order.updateString("firstName", "Avery");
        order.updateString("orderNumber", "1042");
        order.updateString("items[0].name", "Notebook");
        order.updateInt("items[0].quantity", 2);
        order.updateString("items[1].name", "Pen");
        order.updateInt("items[1].quantity", 1);
        // Remove this key to try the missing-field case.
        boolean tryMissingField = false;
        if (tryMissingField) {
            order.delete("orderNumber");
        }

        JsonObject preview = new JsonObject();
        preview.updateString("TemplateData", order.emit());

        rest.addHeader("Content-Type", "application/json");
        String responseText;
        try {
            responseText = rest.fullRequestString("POST", "/v2/email/templates/OrderReceipt/render", preview.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(response.stringOf("RenderedTemplate"));
    }
}