Spring boot
Imal Perera  

Spring Boot Simple Mail Config

Spread the love

Step 1

Add Springboot Mail Dependency in pom.xml

Spring Boot has encapsulated all the mail related dependencies into a starter module and we just have to add this starter dependency in our project pom XML.


<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

Step 2

Do the configurations in the application.properties file


spring.mail.host=smtp.gmail.com
[email protected]
spring.mail.password=examplepassword
spring.mail.properties.mail.smtp.starttls.enable=true

Step 3

Just Write the code.


@RestController
public class SampleController {
 
    @Autowired
    private JavaMailSender mailSender;
 
    @RequestMapping(value = "/sendEmail", method = RequestMethod.GET)
    public void sendMail(){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo("[email protected]");
        message.setSubject("hellow");
        message.setText("mailbody");
        mailSender.send(message);
 
    }
 
}


Let me know in the comments if you like this small article also visit the home page for more short but useful articles like this

Leave A Comment