Spring Boot is a popular framework for building robust and scalable web applications. One of the most common requirements in web applications is the ability to send email notifications to users. In this blog post, we will discuss how to send email via SMTP in Spring Boot.

SMTP stands for Simple Mail Transfer Protocol, and it is the protocol used for sending email messages over the internet. To send email via SMTP in Spring Boot, we need to use the JavaMail API, which provides a set of classes for sending and receiving email messages.
Setting up SMTP configuration in Spring Boot
Before we start sending emails, we need to configure the SMTP settings in our Spring Boot application. We can do this by adding the following properties to our application.properties or application.yml file:
spring.mail.host=smtp.gmail.com spring.mail.port=587 spring.mail.username=<your-email-address> spring.mail.password=<your-email-password> spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true
In the above configuration, we are using Gmail as our SMTP server. We have provided the SMTP host and port, along with our email address and password. We have also enabled authentication and TLS encryption for secure communication.
Sending Email in Spring Boot
Once we have configured the SMTP settings, we can send email messages using the JavaMail API. In Spring Boot, we can use the JavaMailSender interface provided by the Spring framework to send email messages.
To use JavaMailSender, we need to create an instance of the JavaMailSenderImpl class, which is an implementation of the JavaMailSender interface. We can then use this instance to create and send email messages.
Here is an example of how to send a simple email message using JavaMailSender:
@Autowired private JavaMailSender javaMailSender; public void sendEmail() { SimpleMailMessage message = new SimpleMailMessage(); message.setTo("[email protected]"); message.setSubject("Test Email"); message.setText("This is a test email"); javaMailSender.send(message); }
In the above example, we have injected the JavaMailSender instance using the @Autowired annotation. We have then created a SimpleMailMessage object and set the recipient, subject, and body of the email. Finally, we have used the javaMailSender instance to send the email message.
Sending HTML email messages
In addition to sending plain text email messages, we can also send HTML email messages using JavaMailSender. To send HTML email messages, we need to use the MimeMessageHelper class, which is a helper class provided by the JavaMail API.
Here is an example of how to send an HTML email message using JavaMailSender:
@Autowired private JavaMailSender javaMailSender; public void sendEmail() throws MessagingException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setTo("[email protected]"); helper.setSubject("Test Email"); helper.setText("<html><body><h1>This is a test email</h1><p>Hello, world!</p></body></html>", true); javaMailSender.send(message); }
In the above example, we have created a MimeMessage object and used the MimeMessageHelper class to set the recipient, subject, and body of the email. We have set the body of the email to an HTML string and specified that it should be treated as HTML by setting the “true” flag in the setText() method.
Sending HTML email messages with attachment
In addition to sending plain text and HTML email messages, we may also need to attach files to our email messages. To send an HTML email message with an attachment, we can use the MimeMessageHelper class along with the JavaMailSender interface.
Here is an example of how to send an HTML email message with an attachment using JavaMailSender:
@Autowired private JavaMailSender javaMailSender; public void sendEmailWithAttachment() throws MessagingException, IOException { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo("[email protected]"); helper.setSubject("Test Email with Attachment"); String htmlContent = "<html><body><h1>This is a test email</h1><p>Hello, world!</p></body></html>"; helper.setText(htmlContent, true); ClassPathResource file = new ClassPathResource("example.pdf"); helper.addAttachment("example.pdf", file); javaMailSender.send(message); }
In the above example, we have created a MimeMessage object and used the MimeMessageHelper class to set the recipient, subject, and body of the email. We have set the body of the email to an HTML string and specified that it should be treated as HTML by setting the “true” flag in the setText() method.
We have also attached a PDF file to the email message using the addAttachment() method of the MimeMessageHelper class. The file is loaded from the classpath using the ClassPathResource class.
Conclusion
In this blog post, we have discussed how to send email via SMTP in Spring Boot using the JavaMail API. We have covered how to configure SMTP settings and send plain text and HTML email messages. We have also demonstrated how to send an HTML email message with an attachment. By following these steps, you can easily add email functionality to your Spring Boot application.