gpt4 book ai didi

spring-boot - 要让Spring Boot应用程序通过AWS SES发送简单的电子邮件,需要执行哪些配置步骤?

转载 作者:行者123 更新时间:2023-12-04 03:25:51 29 4
gpt4 key购买 nike

我今天已经为此战斗了几个小时。我从http://cloud.spring.io/spring-cloud-aws/spring-cloud-aws.html#_sending_mails的文档开始,它并没有对具体步骤说太多。只是说开发人员可以包括Bean XML,然后 Autowiring 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'

对于此错误,我尝试将 @Qualifier("simpleMailSender")批注添加到 @Autowired,但这没有帮助。

我希望有人能够引导我朝正确的方向发展。

最佳答案

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

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

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

    希望能帮助到你。

    编辑:

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

    像这样:
        @Bean
    public JavaMailSender mailSender(AmazonSimpleEmailService ses) {
    return new SimpleEmailServiceJavaMailSender(ses);
    }

    关于spring-boot - 要让Spring Boot应用程序通过AWS SES发送简单的电子邮件,需要执行哪些配置步骤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46245538/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com