【问题标题】:Configuring mail sender using Java configuration in Spring Cloud AWS在 Spring Cloud AWS 中使用 Java 配置配置邮件发件人
【发布时间】:2019-12-05 19:04:04
【问题描述】:

我想使用 Amazon SES、Spring Cloud AWS 和 Spring Boot 2.1.5 发送电子邮件。

documentation中,它提供了XML来配置邮件发送者。有什么方法可以使用 Java 配置而不是 XML?

【问题讨论】:

    标签: spring-boot spring-cloud-aws


    【解决方案1】:

    让它工作,这就是我所做的。

    1. 构建依赖项
        implementation 'org.springframework.boot:spring-boot-starter-mail'
        implementation 'org.springframework.cloud:spring-cloud-starter-aws'
        implementation 'com.amazonaws:aws-java-sdk-ses'
    
    1. 配置 bean
    @Configuration
    public class AwsConfig {
    
        @Bean
        public AmazonSimpleEmailService amazonSimpleEmailService(AWSCredentialsProvider credentialsProvider) {
             return AmazonSimpleEmailServiceClientBuilder.standard()
                .withCredentials(credentialsProvider)
                .withRegion(Regions.EU_WEST_1).build();
        }
    
        @Bean
        public MailSender mailSender(AmazonSimpleEmailService ses) {
            return new SimpleEmailServiceMailSender(ses);
        }    
    }
    
    1. 通知服务
    @Service
    public class NotificationService {
    
        @Autowired
        private MailSender mailSender;
    
        public void sendMailMessage() {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom("sender@mail.com");
            simpleMailMessage.setTo("to@mail.com");
            simpleMailMessage.setSubject("test subject");
            simpleMailMessage.setText("test text");
            this.mailSender.send(simpleMailMessage);
        }
    }
    
    1. application.yml
    cloud:
      aws:
        credentials:
          accessKey: <YOUR_ACCESS_KEY>
          secretKey: <YOUR_SECRET_KEY>
        stack:
          auto: false
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我将配置文件更改为这个,它工作正常。我希望它有帮助

      @Configuration
      public class AwsMailConfig {
      
        @Bean
        public AmazonSimpleEmailService amazonSimpleEmailService() {
      
          BasicAWSCredentials basicAWSCredentials =
              new BasicAWSCredentials(
                  <AWS_ACCESS_KEY>,<AWS_SECRET_KEY>);
      
          return AmazonSimpleEmailServiceClientBuilder.standard()
              .withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
              .withRegion(Regions.EU_WEST_1)
              .build();
        }
      
        @Bean
        public JavaMailSender javaMailSender(AmazonSimpleEmailService amazonSimpleEmailService) {
          return new SimpleEmailServiceJavaMailSender(amazonSimpleEmailService);
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-06-14
        • 2021-04-18
        • 1970-01-01
        • 2015-08-14
        • 2019-03-28
        • 1970-01-01
        • 2016-12-24
        • 2019-04-23
        • 1970-01-01
        相关资源
        最近更新 更多