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

dei*_*jer 8 amazon-ses spring-boot aws-java-sdk spring-cloud-aws

今天我一直在和它斗争几个小时.我开始使用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'
Run Code Online (Sandbox Code Playgroud)

如果我尝试添加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'
Run Code Online (Sandbox Code Playgroud)

相反,如果我尝试显式的增加在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'
Run Code Online (Sandbox Code Playgroud)

对于这个错误,我尝试添加一个@Qualifier("simpleMailSender")注释@Autowired,但它没有帮助.

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

ska*_*dya 9

您可以尝试按照以下步骤解决问题。我在您的分叉存储库中尝试了这些更改,它对我有用。

  1. pom.xml文件中添加依赖项“ com.amazonaws:aws-java-sdk-ses” 。
  2. 创建一个自动配置类以配置邮件发件人Bean。以下是示例。它AWSCredentialsProvider是由spring-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);
    }
}
Run Code Online (Sandbox Code Playgroud)

3.使用spring API通过配置的邮件发件人发送邮件。

希望能帮助到你。

编辑:

如果你需要使用JavaMailSender的,而不是MailSender(当你想发送附件举例),只需配置SimpleEmailServiceJavaMailSender代替SimpleEmailServiceMailSender

像这样:

    @Bean
    public JavaMailSender mailSender(AmazonSimpleEmailService ses) {
        return new SimpleEmailServiceJavaMailSender(ses);
    }
Run Code Online (Sandbox Code Playgroud)