DEV Community

Cover image for How to send Email In SpringBoot | SpringBoot Tutorial
vatana7
vatana7

Posted on

How to send Email In SpringBoot | SpringBoot Tutorial

This is a latest guide on how to send Email in your SpringBoot Application. Simply follow these steps to make your Spring Application be able to send Email.

Step 1: Add Project Dependency

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Step 2: Set up App Password inside your Google Account

  • Go to (Google Account)[https://myaccount.google.com]
  • On Search Bar, search for App Passwords then you should see App Passwords with Security under it
  • Under Select App, find Other (Custom Name), input your desired name then click Generate
  • Copy the password we will use it later

Step 3: Set up Application.properties

spring.mail.username=youremail@gmail.com
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.password=Copied Password From App Password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.transport.protocol=smtp
spring.mail.properties.mail.smtp.starttls.enable=true
Enter fullscreen mode Exit fullscreen mode

Step 4: Configure Bean for JavaMailSender

@Configuration
public class MailConfiguration {

    @Bean
    public JavaMailSender getJavaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.gmail.com");
        mailSender.setPort(587);

        mailSender.setUsername("your@gmail.com");
        mailSender.setPassword("copiedPassword");

        Properties props = mailSender.getJavaMailProperties();
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.debug", "true");

        return mailSender;
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Implement MailSenderService

@Service
public class MailSenderService {
    @Autowired
    private JavaMailSender mailSender;

    public void sendNewMail(String to, String subject, String body) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(body);
        mailSender.send(message);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 6: Start sending email

private final MailSenderService mailService;

public void Foo(){
 mailService.sendNewMail("test@gmail.com", "Subject right here", "Body right there!")
}
Enter fullscreen mode Exit fullscreen mode

I hope this guide will help you. Thanks for reading!

Oldest comments (7)

Collapse
 
akinwalehabib profile image
Akinwale Folorunsho Habibullah

Thanks for sharing @vatana7 ,

I would add spring.mail.properties.smtp.ssl.enable=true

Chances are that SMTP connection will fail without this when sending emails.

Collapse
 
vatana7 profile image
vatana7

Thanks!

Collapse
 
igormeshalkin profile image
Igor Meshalkin

All guides must be like this.

Collapse
 
ahmed_aminebouricha_21fb profile image
Ahmed Amine Bouricha • Edited

i think doing step 4 is enough without doing the Step 3,isn't ?
because the Java configuration class will override the settings from the application properties or YAML file

and thanks for the guide

Collapse
 
vatana7 profile image
vatana7

You are correct, thanks for the correction

Collapse
 
__2b1dd10 profile image
Сергей Немчинский

the code is exactly the same, but I still get the error Failed message 1: org.eclipse.angus.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;

Collapse
 
vatana7 profile image
vatana7

Did you follow step 2 correctly? It should be able to send if follow these steps