Spring Boot中如何使用SMTP发送电子邮件呢?
下文笔者讲述SpringBoot发送电子邮件的方法分享,如下所示
application.properties
实现思路: 1.引入spring-boot-starter-mail即会自动引入相应的依赖 2.设置相应的配置信息 3.使用JavaMailSender接口发送邮件例:java发送电子邮件的示例分享
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>spring-boot-send-email</artifactId> <packaging>jar</packaging> <name>Spring Boot Send Email</name> <url>https://www.java265.com</url> <version>1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- send email --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> </dependencies> <build> <plugins> <!-- Package as an executable jar/war --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> </plugin> </plugins> </build> </project>
项目依赖项查看
$ mvn dependency:tree [INFO] org.springframework.boot:spring-boot-send-email:jar:1.0 [INFO] +- org.springframework.boot:spring-boot-starter:jar:2.1.2.RELEASE:compile [INFO] | +- org.springframework.boot:spring-boot:jar:2.1.2.RELEASE:compile [INFO] | | \- org.springframework:spring-context:jar:5.1.4.RELEASE:compile [INFO] | | +- org.springframework:spring-aop:jar:5.1.4.RELEASE:compile [INFO] | | \- org.springframework:spring-expression:jar:5.1.4.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-classic:jar:1.2.3:compile [INFO] | | | +- ch.qos.logback:logback-core:jar:1.2.3:compile [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:compile [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.4.RELEASE:compile [INFO] | \- org.yaml:snakeyaml:jar:1.23:Runtime [INFO] \- org.springframework.boot:spring-boot-starter-mail:jar:2.1.2.RELEASE:compile [INFO] +- org.springframework:spring-context-support:jar:5.1.4.RELEASE:compile [INFO] | \- org.springframework:spring-beans:jar:5.1.4.RELEASE:compile [INFO] \- com.sun.mail:javax.mail:jar:1.6.2:compile [INFO] \- javax.activation:activation:jar:1.1:compile
Gmail SMTP
smtp服务器配置application.properties
spring.mail.host=smtp.gmail.com spring.mail.port=587 spring.mail.username=username spring.mail.password=password # Other properties 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, post 465 #spring.mail.properties.mail.smtp.socketFactory.port = 465 #spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
注意事项: Gmail SMTP 当帐户启用两步验证 需创建一个“应用密码”
发送电子邮件
发送普通文本电子邮件
import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; @Autowired private 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("java265.com \n Spring Boot Email"); javaMailSender.send(msg); }
发送HTML电子邮件和文件附件
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() throws MessagingException, IOException { MimeMessage msg = javaMailSender.createMimeMessage(); // true = multipart message MimeMessageHelper helper = new MimeMessageHelper(msg, true); helper.setTo("to_@email"); helper.setSubject("Testing from Spring Boot"); // default = text/plain //helper.setText("Check attachment for image!"); // true = text/html helper.setText("<h1>Check attachment for image!</h1>", true); // hard coded a file path //FileSystemResource file = new FileSystemResource(new File("path/test.png")); helper.addAttachment("test.png", new ClassPathResource("test.png")); javaMailSender.send(msg); }
SpringBoot发送电子邮件示例
package com.java265; 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 Application implements CommandLineRunner { @Autowired private JavaMailSender javaMailSender; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) { System.out.println("Sending Email..."); try { sendEmail(); //sendEmailWithAttachment(); } catch (MessagingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Done"); } void sendEmail() { SimpleMailMessage msg = new SimpleMailMessage(); msg.setTo("1@gmail.com", "2@yahoo.com"); msg.setSubject("Testing from Spring Boot"); msg.setText("java265.com \n Spring Boot Email"); javaMailSender.send(msg); } void sendEmailWithAttachment() throws MessagingException, IOException { MimeMessage msg = javaMailSender.createMimeMessage(); // true = multipart 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 for image!"); // true = text/html helper.setText("<h1>Check attachment for image!</h1>", true); helper.addAttachment("test.png", new ClassPathResource("test.png")); javaMailSender.send(msg); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。