1. Overview
In this article I’ll show you how you can send email from your spring boot app using JavaMailSender interface. It’s an abstraction for sending emails from spring boot that provides auto configuration and easy implementation. I’m using a gmail account here to demonstrate the process.
2. Depencency
Add spring-boot starter-mail dependency on your pom.xml file.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
3. Mail Properties
Add your email credentials on your application.properties file.
#spring-boot-starter-mail properties email.host=smtp.gmail.com email.port=587 [email protected] email.password=yourpassword
4. Configuration
Write a configuration class so that you can configure your JavaMailSender bean with your email credentials and properties.
@Configuration public class MailConfig { @Value("${email.host}") private String host; @Value("${email.port}") private Integer port; @Value("${email.username}") private String username; @Value("${email.password}") private String password; @Bean public JavaMailSender javaMailService() { JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); javaMailSender.setHost(host); javaMailSender.setPort(port); javaMailSender.setUsername(username); javaMailSender.setPassword(password); javaMailSender.setJavaMailProperties(getMailProperties()); return javaMailSender; } private Properties getMailProperties() { Properties properties = new Properties(); properties.setProperty("mail.transport.protocol", "smtp"); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.smtp.starttls.enable", "true"); properties.setProperty("mail.debug", "false"); return properties; } }
If you defined your email properties into another properties file like “mail.properties” you need to add @PropertySource(“classpath:mail.properties”) on your class.
5. Email Service – MailService.java
Okay, now that we’ve configured our JavaMailSender bean with our credentials. We can use this bean to send email. Write a MailService.java class that sends email to an address.
@Service public class MailService { @Autowired private JavaMailSender javaMailSender; public void sendEmail(String email,String message) throws Exception{ SimpleMailMessage mailMessage=new SimpleMailMessage(); mailMessage.setTo(email); mailMessage.setSubject("Response from Autism Barta"); mailMessage.setText(message); javaMailSender.send(mailMessage); } }
6. Use MailService Bean to Send Email
Now everything is ready, you can now inject your MailService bean to your class and send email using it’s sendEmail() method.
@Controller public class EmailController { @Autowired private MailService mailService; @RequestMapping(value = "/email/send", method = RequestMethod.POST) private String sendEmailToUser(@ModelAttribute User user, @RequestParam("message") String message) { try { this.mailService.sendEmail(user.getEmail(), message); } catch (Exception e) { // handle your exception when it fails to send email } return "redirect:/"; } }
7. Conclusion
That was easy, right? May be you may like some other articles like deploying Spring Boot app using Docker, or Authenticating users using Spring Security.
Happy coding.