email gmail java mail Latest smtp spring boot

Spring Boot – How to Send Email via SMTP – Mkyong.com

Spring Start e-mail

On this tutorial we’ll show you ways to ship e-mail via SMTP in the spring

Methods used:

  • Spring Start 2.1.2.RELEASE
  • Spring 5.1. 4. RELEASE
  • Java Mail 1.6.2
  • Maven three
  • Java 8

1. Challenge Directory

  Project Directory

2. Maven

To send an e-mail, you’ll be notified of the spring-boot-starter-mail, it’s going to drag the JavaMail dependencies.

pom.xml

4.0.zero

Spring-boot-send-e mail
jar
Spring Boot Send Email
https://www.mkyong.com
1, 0 [19659020] org.springframework.boot
spring-play-starter
2.1.2.RELEASE

1.8

org.springframework.boot
spring-play starter

] org. springframework.boot
spring-boot-boot-mail

org.springframework.boot
spring-boot-maven plugin

org.apache.maven.plugins
maven-confident plugin [19659032] 2.22.zero

Show venture dependencies

$ mvn dependence: tree

[INFO] org.springframework.boot: spring-boot-ship-e mail: jar: 1.0
[INFO] + – org.springframework.boot: spring-boot-starter: jar: 2.1.2.RELEASE: assemble
[INFO] | + – org.springframework.boot: spring-boot: jar: 2.1.2.RELEASE: compile
[INFO] | | – org.springframework: spring-context: jar: 5.1.4.RELEASE: put collectively
[INFO] | | + – org.springframework: spring-aop: jar: 5.1.four.RELEASE: compile
[INFO] | | – org.springframework: spring-expression: jar: 5.1.four.RELEASE: compile
[INFO] | + – org.springframework.boot: spring-boot-autoconfigure: jar: 2.1.2.RELEASE: compile
[INFO] | + – org.springframework.boot: spring-boot-starter-logging: jar: 2.1.2.RELEASE: compile
[INFO] | | + – ch.qos.logback: logback-basic: jar: 1.2.3: translate
[INFO] | | | + – ch.qos.logback: logback-core: jar: 1.2.3: translate
[INFO] | | | – org.slf4j: slf4j-api: jar: 1.7.25: compile
[INFO] | | + – org.apache.logging.log4j: log4j-to-slf4j: jar: 2.11.1: compile
[INFO] | | | – org.apache.logging.log4j: log4j-api: jar: 2.11.1: compile
[INFO] | | – org.slf4j: jul-to-slf4j: jar: 1.7.25: translate
[INFO] | + – javax.annotation: javax.annotation-api: jar: 1.3.2: compile
[INFO] | + – org.springframework: spring-core: jar: 5.1.4.RELEASE: compile
[INFO] | | – org.springframework: spring-jcl: jar: 5.1.four.RELEASE: put together
[INFO] | – org.yaml: snakeyaml: jar: 1.23: runtime

[INFO] org.springframework.boot: spring-boot-starter-mail: jar: 2.1.2.RELEASE: assemble
[INFO] + – org.springframework: spring-context-help: jar: 5.1.4.RELEASE: compile
[INFO] | – org.springframework: spring-beans: jar: 5.1.4.RELEASE: put collectively
[INFO] com.sun.mail: javax.mail: jar: 1.6.2: compile
[INFO] – javax.activation: activation: jar: 1.1: assemble

3. Gmail SMTP

This example uses a Gmail SMTP server that has been examined with TLS (port 587) and SSL (port 465). Learn this Gmail SMTP

software.properties

spring.mail.host = smtp.gmail.com
spring.mail.port = 587
spring.mail.username = username
spring.mail.password = password

# Different Features
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.connectiontimeout = 5000
spring.mail.properties.mail.smtp.timeout = 5000
spring.mail.properties.mail.smtp.writetimeout = 5000

# TLS, port 587
spring.mail.properties.mail.smtp.starttls.enable = true

# SSL, message 465
# spring.mail.properties.mail.smtp.socketFactory.port = 465
# spring.mail.properties.mail.smtp.socketFactory.class = javax.internet.ssl.SSLSocketFactory

four. Sending Email

Spring Supplies JavaMailSender Interface to JavaMail APIs

four.1 Sending a Normal SMS

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

@Autowired
personal JavaMailSender javaMailSender;

void sendEmail ()

SimpleMailMessage msg = new SimpleMailMessage ();
msg.setTo ("to_1@gmail.com", "to_2@gmail.com", "to_3@yahoo.com");

msg.setSubject ("Testing from Spring Boot");
msg.setText ("Hello World's Spring Boot Email");

javaMailSender.ship (msg);

4.2 Upload HTML Email and Attachment.

import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

void sendEmailWithAttachment () Throw MessagingException, IOException

MimeMessage msg = javaMailSender.createMimeMessage ();

// true = multi-part message
MimeMessageHelper helper = new MimeMessageHelper (msg, true);

helper.setTo ("to_ @ email");

helper.setSubject ("Testing from Spring Boot");

// default = text / plain
//helper.setText("Check attachment! ");

// true = text / html
helper.setText ("

Check image attachment!

", true;

// exhausting coded file path
// FileSystemResource file = new FileSystemResource (new file ("path / android.png"));

helper.addAttachment ("my_photo.png", new ClassPathResource ("android.png"));

javaMailSender.ship (msg);

5. Demo

5.1 Begin the spring boot and ship an e-mail.

Software.java

package deal com.mkyong;

import org.springframework.beans.manufacturing unit.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 The appliance implements CommandLineRunner

@Autowired
personal JavaMailSender javaMailSender;

public Static void fundamental (String [] args)
SpringApplication.run (Software.class, args);

@Bypass
public void run (String … args)

System.out.println ("Sending Email …");

attempt

ship e mail();
// sendEmailWithAttachment ();

in the hall (MessagingException e)
e.printStackTrace ();
in the corridor (IOException e)
e.printStackTrace ();

System.out.println ("Ready");

void sendEmail ()

SimpleMailMessage msg = new SimpleMailMessage ();
msg.setTo ("1@gmail.com", "2@yahoo.com");

msg.setSubject ("Testing from Spring Boot");
msg.setText ("Hello World's Spring Boot Email");

javaMailSender.send (msg);

void sendEmailWithAttachment () Throw MessagingException, IOException

MimeMessage msg = javaMailSender.createMimeMessage ();

// true = multi-part message
MimeMessageHelper helper = new MimeMessageHelper (msg, true);
helper.setTo ("1@gmail.com");

helper.setSubject ("Testing from Spring Boot");

// default = text / plain
//helper.setText("Check attachment! ");

// true = text / html
helper.setText ("

Check image attachment!

", true;

helper.addAttachment ("my_photo.png", new ClassPathResource ("android.png"));

javaMailSender.ship (msg);

Terminal

. ____ _ __ _ _
/ ___ & # 39; _ __ _ _ (_) _ __ __ _
(() ___ | & # 39; _ | & # 39; _ | | & # 39; _
/ ___) | | _) | | | | | || (_ | |))))
& # 39; | ____ | .__ | _ | | _ | _ | | _ __ | / / /
========= | _ | ============== | ___ / = / _ / _ / _ /
:: Spring Boot :: (v2.1.2.RELEASE)

Sending e mail …
Made

Obtain Source

References