Mail Spring Boot

By | | Updated : 2021-12-04 | Viewed : 241 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.

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

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.

SpringBootEmailAppConfig.java
@Bean
    public JavaMailSender javaMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("smtp.gmail.com");
        mailSender.setPort(587);

        mailSender.setUsername("frommygmail@gmail.com");
        mailSender.setPassword("frommygmailpassword");

        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.

SimpleMailService.java
@Component
public class SimpleMailService {

    @Autowired
    private JavaMailSender javaMailSender;

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

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("frommygmail@gmail.com");
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        javaMailSender.send(message);

    }
}

We will write a controller method for mail testing. In real mail will be triggered as part logic.

MailController.java
@GetMapping("/sendMail")
    public void sendMail(){
        simpleMailService.sendSimpleMessage("tomygmail@gmail.com","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.

AttachedMailService.java
@Component
public class AttachedMailService {

    @Autowired
    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);

        helper.setFrom("frommygmail@gmail.com");
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(text);

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

        javaMailSender.send(message);

    }
}
MailController.java
@GetMapping("/sendMailWithAttachment")
    public void sendMailWithAttachment() throws MessagingException {
        attachedMailService.sendMessageWithAttachment("tomygmail@gmail.com","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