How to send email in Spring Boot Project via SMTP

To send an email using SMTP in Spring MVC or Spring Boot projects is pretty simple. In this post, I am going to share an easy method to send email in Spring projects.

Add Maven Dependency

black and gray digital device
Photo by Torsten Dettlaff on Pexels.com (How to Send An Email in Spring Boot Project)

Add spring-boot-starter-mail dependency on your project. It has all the packages that you require to send an email in Spring or Spring Boot project.

Here is a code snippet showing examples of how to add spring-boot-starter-mail to your project. Add the following code snippet to your pox.xml file.

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
</dependency>Code language: HTML, XML (xml)

Modify Application.properties file

If you are done importing the maven package to your project, now you are ready to configure how to send an email from your Spring project.

To do this, first, modify your application.properties file as shown below:

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=username
spring.mail.password=password

# Other properties
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000

# TLS , port 587
spring.mail.properties.mail.smtp.starttls.enable=true

# SSL, post 465
#spring.mail.properties.mail.smtp.socketFactory.port = 465
#spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactoryCode language: PHP (php)

Sending Email using JavaMailSender

Here is an example showing how to use JavaMailSender to send emails in Spring Boot.

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

@Autowired
private JavaMailSender javaMailSender;
	
	void sendEmail() {

        SimpleMailMessage msg = new SimpleMailMessage();
        msg.setTo("to_1@gmail.com", "to_2@gmail.com", "to_3@yahoo.com");

        msg.setSubject("Testing from Spring Boot");
        msg.setText("Hello World \n Spring Boot Email");

        javaMailSender.send(msg);

}Code language: Java (java)

Sending Email With Attachments

To send an email with attachments in Spring project, we should use a MIME multipart message from the JavaMail library instead of SimpleMailMessage (shown in the code snippet above).

Spring supports JavaMail messaging with the org.springframework.mail.javamail.MimeMessageHelper class.

@Override
public void sendMessageWithAttachment(
  String to, String subject, String text, String pathToAttachment) {
    // ...
    
    MimeMessage message = emailSender.createMimeMessage();
     
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    

    helper.setTo("to_1@gmail.com");
    helper.setSubject("Testing from Spring Boot");
    helper.setText("Hello World \n Spring Boot Email");
        
    FileSystemResource file 
      = new FileSystemResource(new File(pathToAttachment));
    helper.addAttachment("Invoice", file);

    emailSender.send(message);
    // ...
}Code language: JavaScript (javascript)

Leave a Reply

Discover more from BHUTAN IO

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top