我没有找到任何有关配置 GlassFish 的 JavaMail 以使用 Amazon SES 发送电子邮件的明确文档。有人可以举个例子吗?
最佳答案
AWS JDK内您可以在以下位置找到示例:samples\AmazonSimpleEmailService\AWSJavaMailSample.java
基本上,您需要将协议(protocol)设置为“aws”,并将用户和密码设置为您的 AWS 凭证:
/*
* Setup JavaMail to use the Amazon Simple Email Service by specifying
* the "aws" protocol.
*/
Properties props = new Properties();
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());
为了发送消息:
// Create a email session
Session session = Session.getInstance(props);
// 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);
这应该适合你。
关于email - 如何配置 GlassFish JavaMail 以使用 Amazon SES?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7898626/