Issue
I have a spring boot application where I have to send alert mails to both Gmail and Zoho mail. Is it possible to send mails to more than one SMTP server using spring email module, since we are configuring the SMTP address in application.yml
Solution
Basically you have to create two instance of the JavaMailSender bean. Assuming that you put both your gmail and zoho configuration in your application.yml, you can create two instance of JavaMailSender like the code below:
@Bean
public JavaMailSender getJavaMailSenderGmail() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.gmail.com");
mailSender.setPort(587);
...other stuff of your config...
return mailSender;
}
@Bean
public JavaMailSender getJavaMailZoho() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.zoho.com");
...other stuff of your config...
return mailSender;
}
Now in your services/controllers/whatyouwant you can autowire your beans.
@Autowired
@Qualifier("javaMailSenderGmail")
JavaMailSender gmail;
@Autowired
@Qualifier("javaMailSenderZoho")
JavaMailSender zoho;
Answered By - rocky
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)