Sending Simple Email Or html Email Via Spring Boot
Posted By : Tarun Tyagi | 30-Jun-2017
Overview:
Sending an e-mail from the backend application part is a quite common use case in the world of enterprise applications. The spring framework gives simple and abstracted way for sending email using JavaMailSender and Spring-Boot provides auto-configuration for it. Now we need to follow these step for integration of spring-boot-starter-mail.
Step 1: Add spring-boot-starter-mail dependency in your pom.xml
org.springframework.boot spring-boot-starter-mail 1.5.3.RELEASE org.springframework.boot spring-boot-starter-thymeleaf
Step 2. Add required information in application.properties file
spring.mail.host=smtp.gmail.com spring.mail.port=587 spring.mail.username=spring.mail.password= spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true
Step 3. Create service
@Service
public class MailServiceImpl implements MailService{
@Autowired
private MailSender mailSender;
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private TemplateEngine templateEngine;
@Autowired
MailProperties mailProperties;
@Override
@Async
public void sendTextMail(String to, String subject, String body, String cc, String bcc) throws MessagingException {
SimpleMailMessage message=new SimpleMailMessage();
message.setTo(to);
message.setText(body);
message.setSubject(subject);
if(cc!=null) message.setCc(cc);
if(bcc!=null) message.setBcc(bcc);
mailSender.send(message);
}
@Override
@Async
public void sendHtmlMail(String to, String subject, String templateName,Context context) throws MessagingException {
MimeMessage mail = javaMailSender.createMimeMessage();
String body = templateEngine.process(templateName, context);
MimeMessageHelper helper = new MimeMessageHelper(mail, true);
try {
helper.setFrom(mailProperties.getProperties().get("from"),mailProperties.getProperties().get("personal"));
} catch (UnsupportedEncodingException e) {
log.error("error in mail service sendHtmlMail method"+e);
}
helper.setTo(to);
helper.setSubject(subject);
helper.setText(body, true);
javaMailSender.send(mail);
}
}
Step 4. Create template resetpassword.html
You have recently requested to reset your password for your Admin Zone Account. Click on the link below to reset it.
The link will be valid for next 2 hrs. If you have not requested for reset password, then please ignore this mail.
Thanks,Step 5. Now you can use these method in your service or in controller for sending the simple mail.
Context context = new Context();
context.setVariable("link","http://xyz.com");
context.setVariable("name",setusername);
try {
mailService.sendHtmlMail(emailid,subject,"resetPassword",context);
} catch (MessagingException e) {
log.error("error in sendForgotPasswordEmail"+e);
}
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Tarun Tyagi
Tarun is a bright Java developer with experience in MEAN stack and Grails frameworks. Other than programming his area of interest are listening music and reading novels.