Spring 4 mail configuration via java config
The code you posted (along with some small improvements to make it more configurable) would be transformed into the following Java config:
@Configuration
public class MailConfig {
@Value("${email.host}")
private String host;
@Value("${email.from}")
private String from;
@Value("${email.subject}")
private String subject;
@Bean
public JavaMailSender javaMailService() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost(host);
return javaMailSender;
}
@Bean
public SimpleMailMessage simpleMailMessage() {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(from);
simpleMailMessage.setSubject(subject);
return simpleMailMessage;
}
}
You should also be aware of the fact that Spring Boot (which you have not mentioned whether or not you are using) can auto-configure an JavaMailSender
for you. Check out this part of the documentation