Java
Java
Amazon SES v2: Send HTML with a plain text alternative
See more Amazon SES Examples
Send a single report notification with an HTML body and a plain text alternative. Each part conveys the same information so the recipient can read it with either kind of mail client. Use a verified sender in the sending Region and grant ses:SendEmail.
Chilkat Java Downloads
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;
// Replace the From address with your verified sender. The destination is the SES mailbox simulator.
JsonObject request = new JsonObject();
request.updateString("FromEmailAddress", "sender+reports@example.com");
request.updateString("Destination.ToAddresses[0]", "success@simulator.amazonses.com");
request.updateString("Content.Simple.Subject.Data", "Weekly report: 12 orders shipped");
request.updateString("Content.Simple.Subject.Charset", "UTF-8");
// SES builds a multipart/alternative message from the two body representations.
request.updateString("Content.Simple.Body.Text.Charset", "UTF-8");
request.updateString("Content.Simple.Body.Text.Data", "Weekly report: 12 orders shipped.");
request.updateString("Content.Simple.Body.Html.Charset", "UTF-8");
request.updateString("Content.Simple.Body.Html.Data", "<h1>Weekly report</h1><p><strong>12 orders</strong> shipped.</p>");
// 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"));
}
}