简体   繁体   中英

Sending Emails with Amazon SES and Spring Boot

I have s SpringBoot app. with this dependencies:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-aws</artifactId>
  <version>2.2.6.RELEASE</version>
</dependency>
<dependency>
  <groupId>io.awspring.cloud</groupId>
  <artifactId>spring-cloud-starter-aws-ses</artifactId>
  <version>2.3.3</version>
</dependency>

this config file:

@Configuration
public class MailConfig {

    @Bean
    public AmazonSimpleEmailService amazonSimpleEmailService() {

        return AmazonSimpleEmailServiceClientBuilder.standard()
                .withCredentials(new ProfileCredentialsProvider("ses-smtp-user.234221-1724219"))
                .withRegion(Regions.EU_WEST_2)
                .build();
    }

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

and

@Service
public class NotificationService {
    
    private final MailSender mailSender;
    private final JavaMailSender javaMailSender;

    public NotificationService(MailSender mailSender, JavaMailSender javaMailSender) {
        this.mailSender = mailSender;
        this.javaMailSender = javaMailSender;
    }

    public void sendMailMessage(
            final SimpleMailMessage simpleMailMessage) {

        this.mailSender.send(simpleMailMessage);
    }

}

but when I start the app. I have this error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 1 of constructor in com.dis.backend.service.NotificationService required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.mail.javamail.JavaMailSender' in your configuration.

Based on the error message, remove the JavaMailSender constructor argument in the NotificationService constructor (assuming you are not using that in the NotificationService ).

In case you wanted to use the JavaMailSender in the NotificationService , you would need to create a Bean of type JavaMailSender which can be injected in the NotificationService . For AWS-SES, you can do so by defining the following bean in your configuration.

@Bean
public JavaMailSender javaMailSender(AmazonSimpleEmailService amazonSimpleEmailService){
    return new SimpleEmailServiceJavaMailSender(amazonSimpleEmailService);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM