1.建项目
2.引入相关依赖
<?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.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>Springboot01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Springboot01</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--引入启动器:spring-boot-starter-mail-->
<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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.编写配置文件(application.properties)
spring.mail.username=1424861923@qq.com
# password处填写授权码
# 授权码是QQ邮箱推出的,用于登录第三方客户端的专用密码
# 适用于登录以下服务:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
spring.mail.password=nnudxohztfotbafa
spring.mail.host=smtp.qq.com
# 开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true
注:授权码的获取可以登录自己的QQ邮箱,点击 设置—账户 进行获取
4.在单元测试中编写邮件发送代码进行发送
package com.bjpowernode;
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.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@SpringBootTest
class Springboot01ApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
// 发送一个简单的邮件
@Test
void contextLoads1() {
SimpleMailMessage mailMessage=new SimpleMailMessage();
mailMessage.setSubject("HelloWorld!");
mailMessage.setText("小馒头爱学Java!");
mailMessage.setTo("1424861923@qq.com");
mailMessage.setFrom("1424861923@qq.com");
mailSender.send(mailMessage);
}
// 发送一个复杂的邮件
@Test
void contextLoads2() throws MessagingException {
MimeMessage mimeMessage=mailSender.createMimeMessage();
// 组装
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
helper.setSubject("HelloWorld");
// 发送的内容格式为HTML
helper.setText("<p style='color:red'>晚安!宝贝</p>",true);
// 添加附件
helper.addAttachment("VCG41N1320830951.jpg",new File("E:\\00java笔记\\创作素材\\VCG41N1320830951.jpg"));
helper.setTo("1424861923@qq.com");
helper.setFrom("1424861923@qq.com");
mailSender.send(mimeMessage);
}
}