Spring Boot应用程序通过AWS SES发送简单电子邮件所需的配置步骤是什么?

11
我今天已经花了几个小时来解决这个问题。我从http://cloud.spring.io/spring-cloud-aws/spring-cloud-aws.html#_sending_mails的文档开始,但它并没有详细说明具体的步骤。它只是说开发人员可以包含一个Bean XML,然后自动装配MailSender。我尝试了许多变体,使用spring-cloud-aws仍然无法使其工作。最后,我直接包含了aws-java-sdk-ses并手动配置了类。
这是一个简单的项目,演示了我尝试过的内容: https://github.com/deinspanjer/aws-ses-test 该项目可以编译,但运行时会出现以下错误:
Parameter 0 of constructor in com.example.awssestest.AwsSesTestApplication required a bean of type 'org.springframework.mail.MailSender' that could not be found.
- Bean method 'mailSender' not loaded because @ConditionalOnClass did not find required class 'javax.mail.internet.MimeMessage'
- Bean method 'simpleMailSender' not loaded because @ConditionalOnClass did not find required class 'com.amazonaws.services.simpleemail.AmazonSimpleEmailService'
- Bean method 'javaMailSender' not loaded because @ConditionalOnClass did not find required class 'com.amazonaws.services.simpleemail.AmazonSimpleEmailService'

如果我尝试添加javax-mail(https://github.com/deinspanjer/aws-ses-test/tree/try-with-javax-mail-api),那么错误会改变为:
Parameter 0 of constructor in com.example.awssestest.AwsSesTestApplication required a bean of type 'org.springframework.mail.MailSender' that could not be found.
- Bean method 'mailSender' not loaded because AnyNestedCondition 0 matched 2 did not; NestedCondition on MailSenderAutoConfiguration.MailSenderCondition.JndiNameProperty @ConditionalOnProperty (spring.mail.jndi-name) did not find property 'jndi-name'; NestedCondition on MailSenderAutoConfiguration.MailSenderCondition.HostProperty @ConditionalOnProperty (spring.mail.host) did not find property 'host'
- Bean method 'simpleMailSender' not loaded because @ConditionalOnClass did not find required class 'com.amazonaws.services.simpleemail.AmazonSimpleEmailService'
- Bean method 'javaMailSender' not loaded because @ConditionalOnClass did not find required class 'com.amazonaws.services.simpleemail.AmazonSimpleEmailService'

如果我尝试显式添加对aws-java-sdk-ses的依赖项(https://github.com/deinspanjer/aws-ses-test/tree/try-with-aws-java-sdk-ses),则会出现以下错误:
Parameter 0 of constructor in com.example.awssestest.AwsSesTestApplication required a bean of type 'org.springframework.mail.MailSender' that could not be found.
- Bean method 'mailSender' not loaded because @ConditionalOnClass did not find required class 'javax.mail.internet.MimeMessage'
- Bean method 'javaMailSender' in 'MailSenderAutoConfiguration' not loaded because @ConditionalOnClass did not find required class 'javax.mail.Session'
- Bean method 'simpleMailSender' in 'MailSenderAutoConfiguration' not loaded because @ConditionalOnMissingClass found unwanted class 'org.springframework.cloud.aws.mail.simplemail.SimpleEmailServiceJavaMailSender'

针对这个错误,我尝试在@Autowired上添加一个@Qualifier("simpleMailSender")注释,但是没有帮助。
我希望有人能够指导我正确的方向。

看起来您在 AwsSesTestApplication 类中漏掉了导入 @ImportResource("/aws-mail.xml")。 - skadya
我刚刚尝试将该注释添加到应用程序类中,但它并没有改变错误消息。 - deinspanjer
5个回答

17

您可以尝试以下步骤来修复问题。我在您的forked repo中尝试了这些更改,它对我有用。

  1. pom.xml文件中添加依赖项"com.amazonaws:aws-java-sdk-ses"
  2. 创建一个自动配置类来配置邮件发送器bean。下面是示例代码。 AWSCredentialsProviderspring-cloud-starter-aws默认配置和提供。

.

@Configuration
public class SimpleMailAutoConfig {

    @Bean
    public AmazonSimpleEmailService amazonSimpleEmailService(AWSCredentialsProvider credentialsProvider) {
        return AmazonSimpleEmailServiceClientBuilder.standard()
                .withCredentials(credentialsProvider)
                // Replace US_WEST_2 with the AWS Region you're using for
                // Amazon SES.
                .withRegion(Regions.US_WEST_2).build();
    }

    @Bean
    public MailSender mailSender(AmazonSimpleEmailService ses) {
        return new SimpleEmailServiceMailSender(ses);
    }
}

3. 使用 Spring API 通过配置的邮件发送器发送邮件。

希望能够帮到你。

编辑:

如果需要使用 JavaMailSender 而不是 MailSender(例如当您想要发送附件时),只需配置 SimpleEmailServiceJavaMailSender 而不是 SimpleEmailServiceMailSender

像这样:

    @Bean
    public JavaMailSender mailSender(AmazonSimpleEmailService ses) {
        return new SimpleEmailServiceJavaMailSender(ses);
    }

1
请问能否帮忙将MailSender定义为JavaMailSender的邮件发送器?我正在尝试使用SES发送HTML邮件,但是MailSender只允许发送纯文本邮件。如何将SES配置指定给JavaMailSender呢? - Master Po
SimpleEmailServiceMailSender是什么?我找不到这个类。 - naXa stands with Ukraine
这个类来自库 https://github.com/spring-cloud/spring-cloud-aws。您可以参考分叉存储库中的 pom.xml 以获取依赖项。 - skadya
需要哪个Spring Boot版本?另外,我如何获取MailSender依赖项? - Sumanth Varada
您可以参考分叉存储库中的pom.xml文件。我认为它是1.5.x版本。 - skadya
SES依赖现已更新为software.amazon.awssdk.ses。 - cherish sham

4

我之前在一个Spring Boot网络项目中使用了AWS SES,但是我并没有使用Spring Cloud AWS来将我的应用程序与邮件服务集成。

相反,我只是在项目依赖项(pom.xml)中包含了spring-boot-starter-mail

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

然后我在我的application.properties中设置了SMTP服务器参数。在我的情况下,我使用这些属性:

spring.mail.host=email-smtp.eu-west-1.amazonaws.com
spring.mail.port=465
spring.mail.protocol=smtps
spring.mail.smtps.auth=true
spring.mail.smtp.ssl.enable=true
spring.mail.username=<my-user>
spring.mail.password=<my-password>

注意: 服务器主机和端口可能会有所不同。

Spring Boot将创建一个默认的JavaMailSender实例,并使用先前的参数进行配置。您可以使用此对象发送电子邮件...

可能这不是最佳的方法来集成AWS SES与Spring Boot应用程序,但对我而言它很有效。


1
我也曾考虑过这样做,但我想避免使用SES的smtp接口。不过还是感谢你的建议,很可能会帮助到其他人。 - deinspanjer
@deinspanjer 你为什么要避免使用SMTP呢?(只是为了了解一下...) - davioooh
1
由于Amazon建议尽可能使用SES API,主要是出于性能考虑。请参阅此文档部分开头的注释:http://cloud.spring.io/spring-cloud-static/spring-cloud-aws/1.2.1.RELEASE/#_sending_mails - deinspanjer
看一下这个Gist:https://gist.github.com/ShigeoTejima/f2997f57405010c3caca 它可能会有用... - davioooh
是的,我看到了这个,它帮助我入门了,但正如评论者所说,自那时以来API已经改变了,它不再直接可用。 - deinspanjer
spring.mail.protocol=smtps 对我很有帮助。谢谢。 - andre

0

你也可以通过Application.properties来完成。确保你不在AWS SES的沙盒中。

  1. 验证你的电子邮件地址或域名以通过SES发送电子邮件。转到SES仪表板并选择“电子邮件地址”(对于测试,您可以从SES仪表板发送演示电子邮件 ->电子邮件地址 ->发送测试电子邮件)。
  2. 创建“访问密钥ID”和“秘密访问密钥”。(没有这些密钥,您将无法发送邮件。您将收到535错误)。

3.现在将密钥放入您的application.properties文件中。如下所示:

`spring.mail.host=email-smtp.us-east-1.amazonaws.com(Your Hosting region)
spring.mail.username=<Access Key Id>
spring.mail.password=<Secret access key>`

如果您正在使用MimeMessage,则将发送邮件ID设置如下:
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setFrom("yourmailID@yourDomain.com");

希望能对你有所帮助。


0
  1. 将 AWS-Java-sdk 添加到您的项目中
  2. 在您的计算机上设置 AWS 凭据(密钥)
  3. 创建 SendEmailRequest
  4. SendEmailRequest#.sendEmail(request)

p.s. 这里不需要使用 JavaMail。您可以以任何所需的方式在 Spring 中包装它为 Bean。


谢谢,我想我应该更清楚地表明我特别是在寻求帮助使spring-cloud-aws像我的示例项目一样工作。 - deinspanjer

0

仅仅是为了补充@davioooh的回答,如果有人使用aws的smtp接口而不是aws sdk api,--

如果您使用的是端口465,则使用协议smtps。但是,如果您使用的是端口25、587,则使用协议smtp。这是强制性的。

如果您在使用端口465时没有使用SMTPS,则会在springboot应用程序中出现异常。(因为在传输邮件时,客户端必须启动TLS,所以您必须在application.properties文件中提到SMTPS。

同样,在使用端口25、587时,您不需要使用SMTPS,因为在这种情况下,服务器首先响应客户端是否启用tls,然后客户端启动tls加密。因此,在端口25、587的情况下,您只需要使用SMTP。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接