效果展示
步骤
1-获取授权码
获取qq或其他第三方登录邮箱发邮件的授权码,相当于qq密码,让别人登录后拿你账号发邮件用的
2-application.yml配置文件中填入对应信息
邮箱和授权码即可,其他默认, debug属性为true可以在控制台输出错误信息
spring:
mail:
# 默认的邮件编码为UTF-8
default-encoding: UTF-8
# 邮箱服务器
host: smtp.qq.com
# 邮箱
username: //你的qq邮箱
# 密码
password: //授权码填写位置
# 端口
port: 587
# 其它属性,这里只开启debug输出错误信息
properties:
debug: true
3-写工具类
package com.example.vueelementson.common;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
/**
* @BelongsProject: blog-springboot
* @BelongsPackage: com.example.vueelementson.common
* @Author: Zww
* @CreateTime: 2024-07-23 09:40
* @Description: TODO
* @Version: 1.0
*/
@Slf4j
@Component
public class MailHelper {
@Value("${spring.mail.username}")
private String from;
// 定义私有常量
private final JavaMailSender javaMailSender;
// 构造函数,设置私有常量JavaMailSender
public MailHelper(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
public void sendMail(String receiver, String subject, String content) {
// 使用Java..sender创建消息对象
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
// 传入
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
try {
// 设置发件时间
mimeMessageHelper.setSentDate(new Date());
// 设置发件来源邮箱
mimeMessageHelper.setFrom(from);
// 设置收件人
mimeMessageHelper.setTo(receiver);
// 设置标题
mimeMessageHelper.setSubject(subject);
// 设置内容
mimeMessageHelper.setText(content);
// 发送
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
log.error(e.getMessage(), e);
}
}
}
4-测试
package com.example.vueelementson;
import com.example.vueelementson.common.MailHelper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* @BelongsProject: blog-springboot
* @BelongsPackage: com.example.vueelementson
* @Author: Zww
* @CreateTime: 2024-07-23 09:55
* @Description: TODO
* @Version: 1.0
*/
@SpringBootTest
public class MailTest {
@Autowired
private MailHelper helper;
@Test
public void testSend() {
helper.sendMail("1626016153@qq.com", "测试", "测试邮件内容");
}
}
5-业务类
在评论保存时发送邮件给站长,异步,否则堵塞占用用户时间
@Autowired
private CommentService commentService;
@Autowired
private ArticleService articleService;
@Autowired
private MailHelper mailHelper;
/**
* 新增评论
*/
@PostMapping("/add")
public Result add(@RequestBody Comment comment) {
// Async注解基于AOP,AOP基于动态代理
// 由于以下方法直接调用本类的异步为this对象内部本身,没有走spring容器代理,类似没有多开一个线程额外的处理异步任务
// sendMailToMaster(comment.getArticleId());
// 也可以使用将该发送方法封装到另一个类,带有component注解并调用也行
Integer articleId = comment.getArticleId();
CommentController bean = SpringUtil.getBean(CommentController.class);
bean.sendMailToMaster(articleId, comment.getCommentContent());
// 判断该评论是否有父评论,有的话给父评论也提个醒
if (comment.getPid() != null) {
Comment repliedComment = commentService.getById(comment.getPid());
if (repliedComment != null && repliedComment.getEmail() != null) {
bean.sendMailToUser(articleId, repliedComment.getEmail());
}
}
return Result.success(commentService.save(comment));
}
@Async
public void sendMailToMaster(Integer articleId, String comment) {
Article byId = articleService.getById(articleId);
if (byId == null) return;
String content = "文章" + byId.getTitle() + "收到新评论,链接为\n" + Website.ARTICLE_URL + articleId + "\n评论内容为:" + comment + "\n时间:" + new Date();
mailHelper.sendMail(Website.MASTER_MAIL, "文章" + byId.getTitle() + "收到新评论", content);
}
@Async
public void sendMailToUser(Integer articleId, String userEmail) {
Article byId = articleService.getById(articleId);
if (byId == null) return;
String content = "你的评论收到了一条回复,链接为\n" + Website.ARTICLE_URL + articleId + "\n时间:" + new Date() + "快去看看吧";
mailHelper.sendMail(userEmail, "在Zww博客上的评论收到一条新回复", content);
}
记得在Application入口类添加@EnableAsync注解