Spring Boot - Email

Spring Boot Email

Maven Dependency

Need to add spring-boot-starter-mail dependency to pom.xml file.

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

Spring has email support to help send email.

spring-boot-starter-mail has dependency on jakarta.mail library. The 2.x version supports jakarta.mail namespace. The 1.x version supports javax.mail namespace. The javax.mail namespace is deprecated.

The interface used for sending email is MailSender interface. It has a simple implementation JavaMailSenderImpl.

Spring auto configures JavaMailSenderImpl bean if it finds spring.mail.* properties in application.properties or application.yml file.

Configuration

Sample application.properties file to configure an outlook email properties.

1
2
3
4
5
6
7
8
9
10
spring.mail.properties.mail.smtp.connecttimeout=5000
spring.mail.properties.mail.smtp.timeout=3000
spring.mail.properties.mail.smtp.writetimeout=5000
spring.mail.host=smtp-mail.outlook.com
spring.mail.port=587
spring.mail.username=xxxx@hotmail.com
spring.mail.password=yourpassword
spring.mail.properties.mail.store.protocol=pop3
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smpt.auth=true

Sending Email

You can just auto wire JavaMailSender and use it to send email.

1
2
@Autowired
private final JavaMailSender emailSender;

Or you can create a new instance of JavaMailSenderImpl and use it to send email.

1
JavaMailSender sender = new JavaMailSenderImpl();

Sending a simple Email

1
2
3
4
5
6
7
8
public void sendSimpleMail() {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo("xxxx@hotmail.com");
message.setFrom("xxxx@hotmail.com");
message.setSubject("Simple Mail Subject");
message.setText("Simple Mail Body");
emailSender.send(message);
}

You can use MimeMessageHelper to help send MimeMessage.

1
2
3
4
5
6
7
8
9
public void sendMimeMailWithHelper() throws MessagingException {
MimeMessage mimeMessage = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, false, "utf-8");
helper.setTo("xxxx@hotmail.com");
helper.setFrom("xxxx@hotmail.com");
helper.setSubject("Mime Message Subject2");
helper.setText("<b>Mime Message Body2</b>", true);
emailSender.send(helper.getMimeMessage());
}

MimeMessageHelper is useful to send email with attachment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void sendMimeMailWithAttachment() throws MessagingException, IOException {
MimeMessage mimeMessage = emailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
helper.setTo("xxxx@hotmail.com");
helper.setFrom("xxxx@hotmail.com");
helper.setSubject("Mime Message With Attachment Subject");
helper.setText("<b>Mime Message With Attachment Subject</b>", true);

Resource resource = new ClassPathResource("attachement.jpeg");
DataSource dataSource = new ByteArrayDataSource(resource.getInputStream(), "image/jpeg");
helper.addAttachment("attachement.jpeg", dataSource);

emailSender.send(helper.getMimeMessage());
}

reference