In today’s post, I will describe how to send an email with spring boot. Post will contain following things:
- Send simple email with simple message
- Send an email with attachment
- Send an email with HTML content
Let’s start!
Firstly create spring boot project and add the following dependency in pom.xml file.
org.springframework.boot spring-boot-starter-mail
Create an application.properties file under src/main/resources and put following properties in it:
spring.mail.host=smtp.gmail.com spring.mail.port =587 spring.mail.default-encoding=UTF-8 spring.mail.password=xxxx spring.mail.username=xxxx spring.mail.protocol=smtp spring.mail.test-connection=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.smtp.auth=true
Let’s create a package (com.threadminions.emailsender in my case) and create following class in it:
MailWithSimpleMessage.java
package com.threadminions.emailsender; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.MailException; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.stereotype.Service; @Service public class MailWithSimpleMessage { @Autowired JavaMailSenderImpl javaMailSenderImpl; public void prepareAndSendMail(String[] to, String message) { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setTo(to); mailMessage.setSubject("Simple Mail"); mailMessage.setText(message); try { javaMailSenderImpl.send(mailMessage); } catch(MailException e) { e.printStackTrace(); } } }
Let’s have an overview of the class:
- JavaMailSenderImpl – It is an instance of implementation class of predefined spring interface JavaMailSender which will automatically configured when spring boot application will start. It will read all the properties defined in application.properties and configure javaMailSenderImpl automatically.
- SimpleMailMessage – It is again a predefined class in spring which is used to prepare message of an email in which we should define the basic parameters of email like to, bcc, cc, text, subject etc. This class is used to transport the plain content in an email and it doesn’t allow modifying message headers. In this example, we only set following properties of it:
- to
- subject
- text
- send() – finally invoke send method of JavaMailSenderImpl class which will send an email and will throw an exception (MailException) in case if anything went wrong.
Let’s create another class which will be used to send an email with attachments as below:
MailWithAttachment.java
package com.threadminions.emailsender; import java.io.File; import javax.mail.internet.MimeMessage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; <span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>import org.springframework.stereotype.Service; @Service public class MailWithAttachment { @Autowired JavaMailSenderImpl javaMailSenderImpl; public void prepareAndSendMail(String[] to, String message) { try { MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage(); MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage, true); mailMsg.setTo(to); mailMsg.setSubject("Test mail with Attachment"); mailMsg.setText(message); FileSystemResource file = new FileSystemResource(new File("G:/spring_boot_dbcp.png")); mailMsg.addAttachment("attachment.png", file); javaMailSenderImpl.send(mimeMessage); } catch (Exception e) { e.printStackTrace(); } } }
Let’s have an overview of this class:
- MimeMessage– – class to prepare complex message with attachments.
- MimeMessageHelper – class is a decorator for MimeMessage that provides more developer friendly interface and adds input validation for many properties of the class.
- FileSystemResource – class to prepare attachments for emails.
- addAttachment() – To add attachments in mail message.
- send() – used to send an email according to configured mime message.
Let’s create another class which will be used to send an email with HTML content as below:
MailWithHtml.java
package com.threadminions.emailsender; import javax.mail.internet.MimeMessage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; @Service public class MailWithHtml { @Autowired JavaMailSenderImpl javaMailSenderImpl; public void prepareAndSendMail(String[] to, String message) { try { MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage(); MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage, true); mailMsg.setTo(to); mailMsg.setSubject("Test mail with Attachment"); mailMsg.setText(message, true); javaMailSenderImpl.send(mimeMessage); } catch (Exception e) { e.printStackTrace(); } } }
It is quite similar like MailWithAttachement.java with the difference of setText() method of MimeMessageHelper class in which we are passing second parameter as true which indicates that whatever message we are sending in an email it will be in HTML format.
Following is the main class to test above code:
SpringBootConfig.java
package com.threadminions.emailsender; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author Threadminions * */ @SpringBootApplication public class SpringBootConfig implements CommandLineRunner { @Autowired MailWithSimpleMessage mailWithSimpleMessage; @Autowired MailWithAttachment mailWithAttachment; @Autowired MailWithHtml mailWithHtml; public static void main(String[] args) { SpringApplication.run(SpringBootConfig.class, args); } @Override public void run(String... args) throws Exception { String[] to = {""}; mailWithSimpleMessage.prepareAndSendMail(to, "Hello, This is simple email from threadminions"); mailWithAttachment.prepareAndSendMail(to, "This is an email with attachments from threadminions"); mailWithHtml.prepareAndSendMail(to," <h1>This is an email with attachments from threadminions</h1> "); } }
This is all about how to send different types of email using spring boot.
Now run this class and it will send different types of email to provided.
Download source code from here