准备工作
登录网页QQ邮箱,点击页面顶部设置按钮,需要在邮箱设置中申请开通 POP3/SMTP 服务。
如何已经开启就点击管理 服务
这里需要一个授权码,例如:uvzpxxxxxbtlwfcbd
到此,准备工作已完成!
二、项目中配置邮件发送功能
<!--邮件发送依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
application.yml文件中添加邮件基本配置
spring: # 发送邮件配置 mail: host: smtp.qq.com # 配置 smtp 服务器地址 port: 587 # smtp 服务器的端口 username: 2032330393@qq.com # 配置邮箱用户名(你的邮箱地址) password: uvzpzirabtlwfcbd # 配置申请到的授权码(刚让复制的授权码) default-encoding: UTF-8 # 配置邮件编码 properties: mail: smtp: socketFactoryClass: javax.net.ssl.SSLSocketFactory # 配饰 SSL 加密工厂 debug: true from: 2032330393@qq.com # 发送方邮件,配在yml中可方便更改 新建一个操作email的工具类(EmailUtil.java)
package com.sby.demo1.config;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import java.io.File;
/**
* @ClassName EmailUtil
* @Description 邮件发送工具
* @Author Sophia
* @Date 2022/4/6 16:06
*/
@Component
public class EmailUtil {
@Value("${spring.mail.from}") // 从application.yml配置文件中获取
private String from; // 发送发邮箱地址
@Autowired
private JavaMailSender mailSender;
/**
* 发送纯文本邮件信息
*
* @param to 接收方
* @param subject 邮件主题
* @param content 邮件内容(发送内容)
*/
public void sendMessage(String to, String subject, String content) {
// 创建一个邮件对象
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom(from); // 设置发送发
msg.setTo(to); // 设置接收方
msg.setSubject(subject); // 设置邮件主题
msg.setText(content); // 设置邮件内容
// 发送邮件
mailSender.send(msg);
}
/**
* 发送带附件的邮件信息
*
* @param to 接收方
* @param subject 邮件主题
* @param content 邮件内容(发送内容)
* @param files 文件数组 // 可发送多个附件
*/
public void sendMessageCarryFiles(String to, String subject, String content, File[] files) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom(from); // 设置发送发
helper.setTo(to); // 设置接收方
helper.setSubject(subject); // 设置邮件主题
helper.setText(content); // 设置邮件内容
if (files != null && files.length > 0) { // 添加附件(多个)
for (File file : files) {
helper.addAttachment(file.getName(), file);
}
}
} catch (MessagingException e) {
e.printStackTrace();
}
// 发送邮件
mailSender.send(mimeMessage);
}
/**
* 发送带附件的邮件信息
*
* @param to 接收方
* @param subject 邮件主题
* @param content 邮件内容(发送内容)
* @param file 单个文件
*/
public void sendMessageCarryFile(String to, String subject, String content, File file) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom(from); // 设置发送发
helper.setTo(to); // 设置接收方
helper.setSubject(subject); // 设置邮件主题
helper.setText(content); // 设置邮件内容
helper.addAttachment(file.getName(), file); // 单个附件
} catch (MessagingException e) {
e.printStackTrace();
}
// 发送邮件
mailSender.send(mimeMessage);
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
}
测试邮件发送功能
package com.sby.demo1.test;
import com.sby.demo1.config.EmailUtil;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.File;
@SpringBootTest
class BlogApiApplicationTests {
@Autowired
private EmailUtil emailUtil;
@Test
void contextLoads() {
}
@Test
void sendStringEmail() {
// 测试文本邮件发送(无附件)
String to = "1463795349@qq.com"; // 这是个假邮箱,写成你自己的邮箱地址就可以
String title = "文本邮件发送测试";
String content = "文本邮件发送测试";
emailUtil.sendMessage(to, title, content);
}
@Test
void sendFileEmail() {
// 测试单个附件邮件发送
String to = "1463795349@qq.com"; // 这是个假邮箱,写成你自己的邮箱地址就可以
String title = "单个附件邮件发送测试";
String content = "单个附件邮件发送测试";
File file = new File("D:\\GCH\\Typora\\Linux中常用的查看系统相关信息命令.md");
emailUtil.sendMessageCarryFile(to, title, content, file);
}
@Test
void sendFilesEmail() {
// 测试多个附件邮件发送
String to = "1463795349@qq.com"; // 这是个假邮箱,写成你自己的邮箱地址就可以
String title = "多个附件邮件发送测试";
String content = "多个附件邮件发送测试";
File files = new File("C:\\Users\\root\\Desktop\\配置邮箱\\1.png");
emailUtil.sendMessageCarryFile(to, title, content, files);
}
}
to是收件邮箱,收到了就代表成功了