Spring Batch Email
spring-boot-starter-mail
Spring Boot has Email feature named spring-boot-starter-mail.
build.gradle
dependencies { compile("org.springframework.boot:spring-boot-starter-mail") }
If you want to support attachment of email, please add java-mail
dependencies { compile("org.springframework.boot:spring-boot-starter-mail") compile("javax.mail:javax.mail-api") }
Tasklet
This is simple tasklet example
@Component @Data public class MailTasklet implements Tasklet { private String from; private String to; private String title; private String message; private JavaMailSender sender; @Autowired public MailTasklet(@Value("${app.mail.from}") final String from, @Value("${app.mail.to}") final String to, @Value("${app.mail.subject}") final String title, @Value("${app.mail.message}") String message) { this.from = from; this.to = to; this.title = title; this.message = message; this.sender = new JavaMailSenderImpl(); } @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { //sendMailWithAttachment(from, to, title, message); sendMail(from, to, title, message); return RepeatStatus.FINISHED; } /* * Helpers */ /* * This is without attachment */ private void sendMail(String from, String to, String title, String message) throws MailException { SimpleMailMessage msg = new SimpleMailMessage(); msg.setFrom(from); msg.setTo(to); msg.setSubject(title); msg.setText(message); this.sender.send(msg); } /* * Mail with attachment */ private void sendMailWithAttachment(String from, String to, String title, String message) throws MailException { MimeMessage mimeMessage = this.sender.createMimeMessage(); try{ MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(title); helper.setText(message); // Works!! ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource("attachment.txt").getFile()); helper.addAttachment("test.txt", file); } catch (MessagingException e) { throw new MailParseException(e); } this.sender.send(mimeMessage); } }
The file location is resources/test.txt
Listener
@Component public class JobEndEmailNotificationListener extends JobExecutionListenerSupport { private String from; private String to; private String title; private String message; private JavaMailSender sender; @Autowired public JobEndEmailNotificationListener(@Value("${app.mail.from}") final String from, @Value("${app.mail.to}") final String to, @Value("${app.mail.subject}") final String title, @Value("${app.mail.message}") String message) { this.from = from; this.to = to; this.title = title; this.message = message; this.sender = new JavaMailSenderImpl(); } @Override public void afterJob(JobExecution jobExecution) { sendMail(from, to, title, message); System.out.println("------------Job Complete-----------------"); } private void sendMail(String from, String to, String title, String message) throws MailException { SimpleMailMessage msg = new SimpleMailMessage(); msg.setFrom(from); msg.setTo(to); msg.setSubject(title); msg.setText(message); this.sender.send(msg); } }
Property
To work with this code, we need properties for smtp server and default values(from, to, etc….)
resources/applicaiton.yml
spring: mail: host: localhost smtp: timeout: 5000 connectiontimeout: 5000 app: mail: from: daiji.ito@rakuten.com to: daiji.ito@rakuten.com subject: Test message: Spring Boot Stareter Mail
How to test it
For Windows, virtual SMTP server software is very useful to test. smtp4dev