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

What is the required configuration steps to have a Spring Boot application send simple e-mails via AWS SES?

今天我已经为此奋斗了几个小时。我从 http://cloud.spring.io/spring-cloud-aws/spring-cloud-aws.html#_sending_mails 的文档开始,它并没有真正说明具体步骤。它只是说开发人员可以包含一个 Bean XML 然后自动装配 MailSender。我已经尝试过它以及许多变体,但无法使用 spring-cloud-aws 使其工作。我最终求助于直接包含 aws-java-sdk-ses 并手动配置 class.

这是一个简单的项目,展示了我的尝试: 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") 注释,但没有帮助。

我希望有人能够引导我朝着正确的方向前进。

前段时间我在一个Spring Boot web 项目中使用了AWS SES,但是我没有使用Spring Cloud AWS 将我的应用程序与邮件服务集成。

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

<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 启动将创建一个默认的 JavaMailSender 实例,并使用之前的参数对其进行配置。您可以使用此对象发送电子邮件...

这可能不是 AWS SES 与 Spring 启动应用程序集成的最佳方法,但对我来说效果很好。

  1. 将 AWS-Java-sdk 添加到您的项目
  2. 在您的机器上设置 AWS 凭证(密钥)
  3. 创建发送电子邮件请求
  4. SendEmailRequest#.sendEmail(请求)

p.s。您在这里不需要任何 JavaMail。您可以以任何需要的方式将其包装到 Spring 中的 beans。

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

  1. pom.xml 文件中添加依赖项 "com.amazonaws:aws-java-sdk-ses"。
  2. 创建一个自动配置 class 来配置邮件发件人 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);
    }

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

  1. 验证您的 E-mail 地址或域,以便通过 SES.Go 向 SES 仪表板和 select "Email Addresses" 发送电子邮件。(为了测试,您可以发送演示电子邮件自SES 仪表板 --> 电子邮件地址 --> 发送测试电子邮件)。
  2. 创建 'Access Key Id' 和 'Secret access key'。(没有这个键你不能发送邮件。你会得到 535-Error)。

3.Now 将钥匙放入您的 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");

希望对您有所帮助。

如果有人在使用 aws 的 smtp 接口而不是 aws sdk api,只是为了向@davioooh 的回答添加一些内容,-- 如果您使用的是 465 端口,则使用协议作为 smtps 但是,如果您使用的是端口 25,587,那么您使用的协议是 smtp 必填。

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

同样,使用端口 25,587 时不需要使用 SMTPS,因为在这种情况下,服务器首先响应客户端它是否启用了 tls,然后客户端发起一个 tls encryption.So,你只在端口 25,587 的情况下必须使用 SMTP。