Java
Java
Amazon SES v2: Publish delivery events to SNS and correlate a tagged send
See more Amazon SES Examples
Use an existing configuration set and Standard SNS topic in the same Region. Grant SES permission to publish to the topic and subscribe your consumer. Configure the destination once, then set configureDestination to false to send a test. In the SES event inside the SNS Message, eventType identifies the outcome, mail.messageId matches the send result, and mail.tags.notification_ref[0] identifies the application record. Use the SES bounce or complaint simulator address to exercise those outcomes.
Configure the SNS destination · SES event payload
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;
boolean configureDestination = true;
String setName = "application-mail";
// 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 (configureDestination) {
StringBuilder path = new StringBuilder();
path.append("/v2/email/configuration-sets/");
path.append(setName);
path.append("/event-destinations");
JsonObject destination = new JsonObject();
destination.updateString("EventDestinationName", "delivery-to-sns");
destination.updateString("EventDestination.MatchingEventTypes[0]", "DELIVERY");
destination.updateString("EventDestination.MatchingEventTypes[1]", "BOUNCE");
destination.updateString("EventDestination.MatchingEventTypes[2]", "COMPLAINT");
destination.updateString("EventDestination.SnsDestination.TopicArn", "arn:aws:sns:us-west-2:123456789012:ses-delivery");
destination.updateBool("EventDestination.Enabled", true);
rest.addHeader("Content-Type", "application/json");
String setupResponse;
try {
setupResponse = rest.fullRequestString("POST", path.getAsString(), destination.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("Destination created. Use the send branch to test it.");
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", "Delivery event test");
request.updateString("Content.Simple.Body.Text.Data", "Exercise the application-mail configuration set.");
request.updateString("EmailTags[0].Name", "notification_ref");
request.updateString("EmailTags[0].Value", "notify-1042");
request.updateString("ConfigurationSetName", setName);
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"));
}
}