Spring Boot: Java Mail Api

Hi Guys,
I would like to share simple but helpful concept to send a mail from boot application.

Spring boot provides a starter module which configure automatically based on provided properties and avail to use, by default spring framework provides abstraction to send mail using JavaMailSender interface.

To do same you have to change below files, here i am using gmail’s configuration for demo.

application.properties

Below is the list of basic properties needs to be set.

spring.mail.host = smtp.gmail.com # SMTP server host.
spring.mail.username = <GMAIL_USERNAME> # Login user of the SMTP server.
spring.mail.password = <GMAIL_PASSWORD> # Login password of the SMTP server.
spring.mail.port=587
spring.mail.properties.mail.smtp.starttls.enable = true

Apart from these properties boot will allow other properties to be override in case of requirement, which are listed below.

spring.mail.default-encoding=UTF-8 # Default MimeMessage encoding.
spring.mail.jndi-name= # Session JNDI name. When set, takes precedence to others mail settings.
spring.mail.port= # SMTP server port.
spring.mail.properties.*= # Additional JavaMail session properties.
spring.mail.protocol=smtp # Protocol used by the SMTP server.
spring.mail.test-connection=false # Test that the mail server is available on startup.

pom.xml

Required below dependency.

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

MailUtil.java

A sample utility class used to combine everything required to send a mail.

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class MailUtil {

    @Autowired
    JavaMailSender javaMailSender;

    @Async
    public Boolean send(String to, String cc, String subject, String body, boolean isHtml) {

        try {

            final MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
            final MimeMessageHelper mail = new MimeMessageHelper(mimeMessage);

            mail.setTo(to);
            if (cc != null) {
                mail.setCc(cc);
            }
            mail.setFrom("no-reply@kode12.com");
            mail.setSubject(subject);
            mail.setText(body, isHtml);
            javaMailSender.send(mimeMessage);

            return true;
        } catch (Exception e) {
            logger.error("Error: ", e);
            return false;
        }
    }
}

The code is pretty simple and easy to understand, invoke send function using

send(“yogeshmprajapati@gmail.com”, null, “Test”, “<h1>Test Body</h1>”, true);

And here we go.

Share current post by copy: https://goo.gl/50PtDR
Happy Learning!
:)

Thanks,
Yogesh P