You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Nathan Maves <na...@gmail.com> on 2011/02/16 05:47:22 UTC
[Email] Using Amazon Simple Email Service SES
Has anyone been able to use this service with commons email? I can't seem
to wrap my head around how to do it.
Here here is bit of their sample code....
PropertiesCredentials credentials = new PropertiesCredentials(
AWSJavaMailSample.class
.getResourceAsStream("AwsCredentials.properties"));
Properties props = new Properties();
/*
* Setup JavaMail to use the Amazon Simple Email Service by
* specifying the "aws" protocol.
*/
props.setProperty("mail.transport.protocol", "aws");
/*
* Setting mail.aws.user and mail.aws.password are optional. Setting
* these will allow you to send mail using the static transport send()
* convince method. It will also allow you to call connect() with no
* parameters. Otherwise, a user name and password must be specified
* in connect.
*/
props.setProperty("mail.aws.user", credentials.getAWSAccessKeyId());
props.setProperty("mail.aws.password", credentials.getAWSSecretKey());
Session session = Session.getInstance(props);
try {
// Create a new Message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(FROM));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(TO));
msg.setSubject(SUBJECT);
msg.setText(BODY);
msg.saveChanges();
// Reuse one Transport object for sending all your messages
// for better performance
Transport t = new AWSJavaMailTransport(session, null);
t.connect();
t.sendMessage(msg, null);
// Close your transport when you're completely done sending
// all your messages
t.close();
}