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.

3. Mail Properties

Add your email credentials on your application.properties file.

4. Configuration

Write a configuration class so that you can configure your JavaMailSender bean with your email credentials and 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.

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.

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.

Leave a Comment