Spring Boot Send Email

Home / Spring Boot Send Email

Sending email is a very basic task in every applications. Spring boot provides JavaMailSender interface that is used for sending Simple text as well as HTML email to end user. So for sending email in spring boot we have to follow some steps and steps are:

  1. Create a spring boot projects using spring initializer with Mail dependency.
  2. Write your email properties in application.properties file.
  3. Create a Controller class and make a POST end point to receive email id.
  4. Create a Service class for sending email to given email id.
  5. we will send both simple text as well as HTML email to given email id.

Project Directory Strucuture

2- Write your email properties in application.properties file.

 spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=vasu@gmail.com
spring.mail.password= Write Your Gmail Account Password
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000

3- Create a Controller class and make a POST end point to receive email id

 @RestController
public class MyController {

@Autowired
private MyService service;

@PostMapping("/sendMail")
public ResponseEntity sendEmail(@RequestParam String emailId) {

ResponseEntity sendResult = service.send(emailId);
return sendResult;
}
}

4- Create a Service class for sending email to given email id.

First We Send a Simple plain Text Message to given Email Id

 @Service
public class MyService {

@Autowired
JavaMailSender mailSender;

public ResponseEntity<?> send(String emailId) {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message);
messageHelper.setTo(emailId);
messageHelper.setSubject("Testing");
messageHelper.setText("Nice Try");
mailSender.send(message);
System.out.println("succesfully SendEmail");
return new ResponseEntity<Object>("succesfully SendEmail",HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<Object>("Error in sending Mail", HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Now Send a HTML message to given email id. For sending HTML email just write your message in HTML formate and set setText boolean flag to true


Eg:
StringBuilder builder = new StringBuilder();
builder.append("<html><h3>Email in HTML</h3></html>");
messageHelper.setText(builder.toString(), true);

@Service
public class MyService {
@Autowired
JavaMailSender mailSender;

public ResponseEntity<?> send(String emailId) {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message);
StringBuilder builder = new StringBuilder();
builder.append("<html><h3>Email in HTML</h3></html>");
messageHelper.setTo(emailId);
messageHelper.setSubject("Testing");
messageHelper.setText(builder.toString(), true);
mailSender.send(message);
System.out.println("succesfully SendEmail");
return new ResponseEntity<Object>("succesfully SendEmail", HttpStatus.OK);
}catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<Object>("Error in sending Mail", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}




Help Others, Please Share

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *

x