email - 如何配置 GlassFish JavaMail 以使用 Amazon SES?

标签 email jakarta-mail glassfish-3 amazon-ses

我没有找到任何有关配置 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/

相关文章:

mysql - 应用程序无法在 ubuntu 上启动,但在 windows 上启动没有任何问题

jakarta-ee - jax-rs Jersey : Exception Mapping for Enum bound FormParam

java - 如何在 JSF 2.0 中创建自定义 404 消息?

email - 在 Powershell 中复制打开的文件

email - mongodb查询持续运行

spring-mvc - 如何在 JUnit 或 Mockito 中检查附件的 MimeMessage?

java - 为什么 JavaMail BodyPart.getInputStream() 在使用 IMAP 时返回空流,而在使用 POP3 时则不返回空流?

email - Outlook 2007 中邮件内容的最后一行

email - 将 Google Apps gmail 与 Symfony nahomail 插件结合使用

java - 使用javaMail读取共享邮箱