Amazon SES sendEmail example

public class AWSeMailService {

public static void send(AmazonSimpleEmailService ses, String from,
String to, String subject, String body) throws IOException {

/*
* Before you can send email via Amazon SES, you need to verify that you
* own the email address from which you'll be sending email. This will
* trigger a verification email, which will contain a link that you can
* click on to complete the verification process.
*/
verifyEmailAddress(ses, from);

// RETERN_PATH is optional

SendEmailRequest sendEmailRequest = new SendEmailRequest()
.withSource(from).withReturnPath(RETERN_PATH);

List toAddresses = new ArrayList<>();
toAddresses.add(to);

Destination destination = new Destination()
.withToAddresses(toAddresses);

sendEmailRequest.setDestination(destination);

com.amazonaws.services.simpleemail.model.Message message

= new com.amazonaws.services.simpleemail.model.Message(new Content()
.withCharset("UTF-8").withData(subject + " goyun.info"), new Body(
new Content().withCharset("UTF-8").withData(body)));
sendEmailRequest.setMessage(message);

ses.sendEmail(sendEmailRequest);

}

/*
* Important: Be sure to fill in an email address you have access to so that
* you can receive the initial confirmation email from Amazon Simple Email
* Service.
*/
private static final String RETERN_PATH = "noreply@goyun.info";
private static final String TO = "nobody@goyun.info";
private static final String FROM = "somebody@goyun.info";
private static final String BODY = "This is an email by java from Amazon Simple Email Service!";
private static final String SUBJECT = "A trying mail from Amazon SES by Java";

public static void main(String[] args) throws IOException {
/*
* Important: Be sure to fill in your AWS access credentials in the
* AwsCredentials.properties file before you try to run this sample.
* http://aws.amazon.com/security-credentials
*/
PropertiesCredentials credentials = new PropertiesCredentials(
AWSeMailService.class
.getResourceAsStream("AwsCredentials.properties"));
AmazonSimpleEmailService ses = new AmazonSimpleEmailServiceClient(
credentials);

AWSeMailService.send(ses, FROM, TO, SUBJECT, BODY);

}

/**
* Sends a request to Amazon Simple Email Service to verify the specified
* email address. This triggers a verification email, which will contain a
* link that you can click on to complete the verification process.
*
* @param ses
* The Amazon Simple Email Service client to use when making
* requests to Amazon SES.
* @param address
* The email address to verify.
*/
private static void verifyEmailAddress(AmazonSimpleEmailService ses,
String address) {

String domain = address.substring(address.indexOf("@") + 1);

List lids = ses.listIdentities().getIdentities();

if (lids.contains(address) || lids.contains(domain)) {
return;
}

ses.verifyEmailAddress(new VerifyEmailAddressRequest()
.withEmailAddress(address));
System.out.println("Please check the email address " + address
+ " to verify it");
System.exit(0);
}
}

Comments

Popular posts from this blog

Check MySQL query history from command line

Installing chocolatey on Windows