1、邮件发送需要引入spring-boot-starter-mail
2、spring boot自动配置MailSenderAutoConfiguration
3、定义MailProperties内容,配置application.yml中
4、自动装配JavaMailSender
5、测试邮件发送
首先需要进入qq邮箱中设置-账户中开启相关服务,其中生成的授权码用作配置文件中的密码:
示例:
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>
<groupId>com.qublog</groupId>
<artifactId>springboot12_task</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot12_task</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.mail.username=邮箱账号
spring.mail.password=生成的授权码
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true
Springboot12TaskApplicationTests类:
package com.qublog.springboot12_task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.internet.MimeMessage;
import java.io.File;
@SpringBootTest
class Springboot12TaskApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
//邮件设置
simpleMailMessage.setSubject("通知今晚开会");
simpleMailMessage.setText("今晚7:30开会");
simpleMailMessage.setTo("目的邮箱@163.com");
simpleMailMessage.setFrom("源邮箱@qq.com");
mailSender.send(simpleMailMessage);
}
@Test
public void test02() throws Exception {
//1、创建一个复杂的消息邮件
MimeMessage message = mailSender.createMimeMessage();
//邮件设置
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setSubject("123456");
helper.setText("<b style='color:red'>123456</b>",true);
helper.setTo("目的邮箱@163.com");
helper.setFrom("源邮箱@qq.com");
//上传文件
helper.addAttachment("springboot_cache.sql",new File("C:\\Users\\Think\\Desktop\\springboot_cache.sql"));
mailSender.send(message);
}
}