I have a situation where I would like to use Gmail SMTP in my environment 1 and Amazon SES in environment 2 to send emails. I know how to achieve each of these individually but can't get it to work for both in such a way that I don't have to do special setups for different environments.
The problem I am struggling with is that for Gmail SMTP, the properties are defined in the application.properties file and I am using auto-configuration like below
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=myEmail
spring.mail.password=myPassword
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
But for SES, I am defining the configuration bean, like below-
@Configuration
public class EmailConfig {
@Bean
public AmazonSimpleEmailService amazonSimpleEmailService() {
return AmazonSimpleEmailServiceClientBuilder.standard()
.build();
}
@Bean
public JavaMailSender javaMailSender(AmazonSimpleEmailService amazonSimpleEmailService) {
return new SimpleEmailServiceJavaMailSender(amazonSimpleEmailService);
}
}
I have tried:
I know the reason why I am struggling with this is my lack of skills. Please help me gain some. Any efficient solution to achieve the desired outcome would be appreciated. Thank you.
In this case, spring boot annotation ConditionalOnProperty comes handy. Define a new property email provider type and use that property to drive which beans to be created