As described in the documentation here we have to set the SMTP info in application.properties file. However, I have a use case where I need to have multiple email account that I can save in the database and use the appropriate email account config when sending email. Is this possible?
Something line this…
public Properties getSmtpProperties(EmailAccount emailAccount) {
final SmtpSetting smtpSettings = dataManager.load(SmtpSetting.class)
.query("select s from msg_SmtpSetting s where s.emailAccount = :emailAccount1 and s.active = true")
.parameter("emailAccount1", emailAccount)
.optional().orElse(null);
if(smtpSettings!=null) {
Properties properties = new Properties();
properties.put("mail.smtp.host", smtpSettings.getHost());
properties.put("mail.smtp.port", smtpSettings.getPort());
properties.put("mail.smtp.auth", smtpSettings.getIsAuth());
properties.put("mail.smtp.starttls.enable", smtpSettings.getIsStarttlsEnable());
properties.put("mail.smtp.username", smtpSettings.getUsername());
properties.put("mail.smtp.password", smtpSettings.getPassword());
properties.put("mail.smtp.from", smtpSettings.getFromAddress());
return properties;
}else{
log.info("SMTP setting info is not found. Please update it in MDG - General");
throw new RuntimeException("SMTP setting info is not found. Please update it in MDG - General");
}
}