Mail Spring Boot

By | | Updated : 2021-12-04 | Viewed : 386 times

Mail Spring Boot

Mailing is one of the crucial parts when implementing applications. Spring Boot provides mail API hence mailing is very easy and Spring Boot reduced boilerplate code as well. We just need to set up an SMTP server, email list, content/attachment. By setting these steps mailing is very easy.

Maven Dependencies

The required maven dependency spring-boot-starter-mail when Spring Boot used. Notice below the given dependency tag for the Maven pom file.


Spring Mail API

  1. MailSender: It is the root interface for Spring Java Mail API to send the mail.

  2. JavaMailSender: It is Sub interface of MailSender which supports MIME messages. Using with JavaMailSender MimeMessageHelper can only populate the MIME message where as MimeMessagePreparator is a callback interface and it can prepare the MIME message.

  3. MimeMessageHelper: It is used with JavaMailSender to populate the MIME message to be sent.

  4. MimeMessagePreparator: It is a callback interface and is useful for preparing the MIME message.

Gmail SMTP Settings

While we are creating the JavaMailSender, SMTP server properties must be added.
    public JavaMailSender javaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();


        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;

Spring Boot Simple Mail

Now we write code the SimpleMail to send the recipients. As we are using Gmail so the recipient must be Gmail.
public class SimpleMailService {

    private JavaMailSender javaMailSender;

    public void sendSimpleMessage(
            String to, String subject, String text) {

        SimpleMailMessage message = new SimpleMailMessage();


We will write a controller method for mail testing. In real mail will be triggered as part logic.
    public void sendMail(){
        simpleMailService.sendSimpleMessage("","test Spring boot Mail", "hi, How are you");

When you call the controller method sendMail() then mail will be triggered.

Spring Boot Mail Attachment

Now we send a mail with an attachment to a particular recipient. See the below-given code example.
public class AttachedMailService {

    private JavaMailSender javaMailSender;

    public void sendMessageWithAttachment(
            String to, String subject, String text, String pathToAttachment) throws MessagingException {

        MimeMessage message = javaMailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message, true);


        FileSystemResource file
                = new FileSystemResource(new File(pathToAttachment));
        helper.addAttachment("teatAttachment.text", file);


    public void sendMailWithAttachment() throws MessagingException {
        attachedMailService.sendMessageWithAttachment("","test Spring boot Mail", "hi, How are you","D:\\docsconsole-git-master\\springboot-master\\Spring-Boot-Email-Example-App\\src\\main\\resources\\teatAttachment.text");

To refer entire code repo, you can see the Github repo Spring-Boot-Email-Example-App.

Leave A Reply