DEV Community

Full Stack Hacker
Full Stack Hacker

Posted on

Spring Boot – How to send email via SMTP

Project Directory

Maven

To send email, declares spring-boot-starter-mail, it will pull the JavaMail dependencies.

  • pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>spring-boot-send-email</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-send-email</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 
Enter fullscreen mode Exit fullscreen mode

Display the project dependencies.

mvn dependency:tree [INFO] Scanning for projects... [INFO] [INFO] -----------------< com.example:spring-boot-send-email >----------------- [INFO] Building spring-boot-send-email 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-dependency-plugin:3.3.0:tree (default-cli) @ spring-boot-send-email --- [INFO] com.example:spring-boot-send-email:jar:0.0.1-SNAPSHOT [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.7.1:compile [INFO] | +- org.springframework.boot:spring-boot-starter:jar:2.7.1:compile [INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:2.7.1:compile [INFO] | | | +- ch.qos.logback:logback-classic:jar:1.2.11:compile [INFO] | | | | \- ch.qos.logback:logback-core:jar:1.2.11:compile [INFO] | | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.17.2:compile [INFO] | | | | \- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile [INFO] | | | \- org.slf4j:jul-to-slf4j:jar:1.7.36:compile [INFO] | | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile [INFO] | | \- org.yaml:snakeyaml:jar:1.30:compile [INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.7.1:compile [INFO] | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.13.3:compile [INFO] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.3:compile [INFO] | | | \- com.fasterxml.jackson.core:jackson-core:jar:2.13.3:compile [INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.13.3:compile [INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.13.3:compile [INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.13.3:compile [INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.7.1:compile [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.64:compile [INFO] | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.64:compile [INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.64:compile [INFO] | +- org.springframework:spring-web:jar:5.3.21:compile [INFO] | | \- org.springframework:spring-beans:jar:5.3.21:compile [INFO] | \- org.springframework:spring-webmvc:jar:5.3.21:compile [INFO] | +- org.springframework:spring-aop:jar:5.3.21:compile [INFO] | +- org.springframework:spring-context:jar:5.3.21:compile [INFO] | \- org.springframework:spring-expression:jar:5.3.21:compile [INFO] +- org.springframework.boot:spring-boot-devtools:jar:2.7.1:runtime [INFO] | +- org.springframework.boot:spring-boot:jar:2.7.1:compile [INFO] | \- org.springframework.boot:spring-boot-autoconfigure:jar:2.7.1:compile [INFO] +- org.springframework.boot:spring-boot-configuration-processor:jar:2.7.1:compile [INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.7.1:test [INFO] | +- org.springframework.boot:spring-boot-test:jar:2.7.1:test [INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.7.1:test [INFO] | +- com.jayway.jsonpath:json-path:jar:2.7.0:test [INFO] | | +- net.minidev:json-smart:jar:2.4.8:test [INFO] | | | \- net.minidev:accessors-smart:jar:2.4.8:test [INFO] | | | \- org.ow2.asm:asm:jar:9.1:test [INFO] | | \- org.slf4j:slf4j-api:jar:1.7.36:compile [INFO] | +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:test [INFO] | | \- jakarta.activation:jakarta.activation-api:jar:1.2.2:test [INFO] | +- org.assertj:assertj-core:jar:3.22.0:test [INFO] | +- org.hamcrest:hamcrest:jar:2.2:test [INFO] | +- org.junit.jupiter:junit-jupiter:jar:5.8.2:test [INFO] | | +- org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test [INFO] | | | +- org.opentest4j:opentest4j:jar:1.2.0:test [INFO] | | | +- org.junit.platform:junit-platform-commons:jar:1.8.2:test [INFO] | | | \- org.apiguardian:apiguardian-api:jar:1.1.2:test [INFO] | | +- org.junit.jupiter:junit-jupiter-params:jar:5.8.2:test [INFO] | | \- org.junit.jupiter:junit-jupiter-engine:jar:5.8.2:test [INFO] | | \- org.junit.platform:junit-platform-engine:jar:1.8.2:test [INFO] | +- org.mockito:mockito-core:jar:4.5.1:test [INFO] | | +- net.bytebuddy:byte-buddy:jar:1.12.11:test [INFO] | | +- net.bytebuddy:byte-buddy-agent:jar:1.12.11:test [INFO] | | \- org.objenesis:objenesis:jar:3.2:test [INFO] | +- org.mockito:mockito-junit-jupiter:jar:4.5.1:test [INFO] | +- org.skyscreamer:jsonassert:jar:1.5.0:test [INFO] | | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test [INFO] | +- org.springframework:spring-core:jar:5.3.21:compile [INFO] | | \- org.springframework:spring-jcl:jar:5.3.21:compile [INFO] | +- org.springframework:spring-test:jar:5.3.21:test [INFO] | \- org.xmlunit:xmlunit-core:jar:2.9.0:test [INFO] \- org.springframework.boot:spring-boot-starter-mail:jar:2.7.1:compile [INFO] +- org.springframework:spring-context-support:jar:5.3.21:compile [INFO] \- com.sun.mail:jakarta.mail:jar:1.6.7:compile [INFO] \- com.sun.activation:jakarta.activation:jar:1.2.2:compile [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.153 s [INFO] Finished at: 2022-07-18T14:40:39+07:00 [INFO] ------------------------------------------------------------------------ 
Enter fullscreen mode Exit fullscreen mode

Gmail SMTP

This example is using Gmail SMTP server, tested with TLS (port 587) and SSL (port 465). Read this Gmail SMTP

  • application.properties

Note

For Gmail SMTP, if your account is enabled the 2-Step verification, please create an App Password

Sending Email

Spring provides an JavaMailSender interface on top of JavaMail APIs.

  • Send a normal text email
  • Send an HTML email and a file attachment.

Demo

Start Spring Boot, and it will send out an email.

package com.example.sendemail; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.io.ClassPathResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.IOException; @SpringBootApplication public class SpringBootSendEmailApplication implements CommandLineRunner { @Autowired private JavaMailSender javaMailSender; public static void main(String[] args) { SpringApplication.run(SpringBootSendEmailApplication.class, args); } @Override public void run(String... args) throws Exception { System.out.println("Sending Email..."); sendEmail(); System.out.println("Done"); } void sendEmailWithAttachment() throws MessagingException, IOException { MimeMessage msg = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(msg, true); helper.setTo("hauntps@gmail.com"); helper.setSubject("Testing from Spring Boot"); helper.setText("<h1>Check attachment for image!</h1>", true); helper.addAttachment("my_photo.png", new ClassPathResource("android.png")); javaMailSender.send(msg); } void sendEmail() { SimpleMailMessage msg = new SimpleMailMessage(); msg.setTo("hauntps@gmail.com", "ngocnt@gmail.com"); msg.setSubject("Testing from Spring Boot"); msg.setText("Hello World \n Spring Boot Email"); javaMailSender.send(msg); } } 
Enter fullscreen mode Exit fullscreen mode


Source code

https://github.com/java-cake/spring-boot/tree/main/spring-boot-send-email

Top comments (0)