Send Mail using Java Mail, Thymeleaf and Spring Boot

Send Mail using Spring boot and Java Mail

In this snippet i used gmail smtp service but feel free to use your smtp of choice.


Maven dependencies


Java Code

Create our mail entity

package com.yamicode.mail.model;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

public class Mail {
    @Pattern(regexp = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
    private String email;
    private String object;
    private String message;

    public String getEmail() {
        return email;

    public void setEmail(String email) { = email;

    public String getObject() {
        return object;

    public void setObject(String object) {
        this.object = object;

    public String getMessage() {
        return message;

    public void setMessage(String message) {
        this.message = message;

The website used to get the email validation regex is: Email Regex

Create our rest controller to test our app.


import com.yamicode.mail.model.Mail;
import com.yamicode.mail.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

public class MailRestController {

    private MailService mailService;

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<?> sendMail(@Valid @RequestBody Mail mail, Errors errors){
            return new ResponseEntity<>(errors.getAllErrors(), HttpStatus.BAD_REQUEST);
        return mailService.sendMail(mail);


  • The @Valid annotation is the one handling our javax.validation.constraints defined in our entity. If an error exists the error will be binded to the errors variable.
package com.yamicode.mail.service;

import com.yamicode.mail.model.Mail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.internet.MimeMessage;

public class MailService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MailService.class);

    private TemplateEngine templateEngine;

    private JavaMailSender javaMailSender;

    public ResponseEntity<?> sendMail(Mail mail){
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);

    public void send(Mail mail) {
    	//get and fill the template
        final Context context = new Context();
        context.setVariable("message", mail.getMessage());
        String body = templateEngine.process("email/email-template", context);
        //send the html template
        sendPreparedMail(mail.getEmail(), mail.getObject(), body, true);

    private void sendPreparedMail(String to, String subject, String text, Boolean isHtml) {
        try {
            MimeMessage mail = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mail, true);
            helper.setText(text, isHtml);
        } catch (Exception e) {
            LOGGER.error("Problem with sending email to: {}, error message: {}", to, e.getMessage());


We used thymeleaf template engine to create our mail

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" xmlns:th="">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Yamicode Email</title>
                <td>Message: </td>
                <td><p th:text="|${message}|">Hello</p></td>

The mail configuration can be set in our application.yml

            enable: true
          auth: true
    password: your-password
    port: 587

Source code:

The full implementation of this artcile can be found in the GitHub project. Download and unzip the source repository for this guide GIT, or clone it using Git: git clone