技术栈 | springboot+mybatis-plus+mysql |
---|---|
软件 | 版本 |
IDEA | IntelliJ IDEA 2022.2.1 |
JDK | 17 |
Spring Boot | 3.1 |
mybatis-plus | 3.5 |
spring-boot-starter-mail | Springboot版本 |
spring boot对mail的封装支持非常好,方便,几行代码就可以把邮件集成进来
spring-boot-starter-mail:
Spring框架提供了一个有用的实用程序库,用于发送电子邮件,使您免受底层邮件系统的限制,并负责代表客户端进行低级资源处理。
该org.springframework.mail软件包是Spring框架的电子邮件支持的根级软件包。用于发送电子邮件的中央界面是该MailSender 界面。封装了简单邮件(例如from和to,以及许多其他邮件)的属性的简单值对象是SimpleMailMessage类。此程序包还包含一个已检查异常的层次结构,该层次结构提供了比较低级别的邮件系统异常更高的抽象级别,根异常为 MailException。
一、QQ邮箱开通开通第三方登入服务
QQ邮箱开通第三方登入服务
POP3/IMAP/SMTP/Exchange/CardDAV 服务已开启
在第三方客户端登录时,密码框请输入以下授权码:
xxxxxxxxxxxxxxxx
pom.xml加入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
application.yml配置
spring:datasource:url: jdbc:mysql://127.0.0.1:3306/mysql?createDatabaseIfNotExist=true&autoReconnect=true&default-character-set=utf8&&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8username: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Drivermail:# 下面这个是QQ邮箱host , 企业邮箱 smtp.exmail.qq.comhost: smtp.qq.com# tencent mail port 这个是固定的port: 465# 你的QQ邮箱username: xxxxxxxqq.com# 进入邮箱配置后得到的授权码password: xxxxxxxxxxxtest-connection: trueproperties:mail:smtp:ssl:enable: true
编写Mail工具类
package com.example.util;import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;import java.io.File;
@Component
public class SendMailUtils {@AutowiredJavaMailSenderImpl javaMailSender;//发送普通文字邮件public void sendText(String Subject, String Text, String setFrom, String setTo) {SimpleMailMessage simpleMailMessage = new SimpleMailMessage();simpleMailMessage.setSubject(Subject);//标题simpleMailMessage.setText(Text);//内容simpleMailMessage.setFrom(setFrom);//发送人邮箱simpleMailMessage.setTo(setTo);//发送目的地邮箱javaMailSender.send(simpleMailMessage);}//发送带页面格式加文件邮件public void sendTexts(String Subject, String Text,Boolean t, String setFrom, String setTo,String attachmentFilename,String filePathName) throws MessagingException {MimeMessage mimeMessage=javaMailSender.createMimeMessage();MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);helper.setSubject(Subject);//标题helper.setText(Text,t);//内容helper.setFrom(setFrom);//发送人邮箱helper.setTo(setTo);//目的地邮箱helper.addAttachment(attachmentFilename,new File(filePathName)); //图片路径javaMailSender.send(mimeMessage);}}
controller调用SendMailUtils工具类
@Autowired
SendMailUtils sendMailUtils;@GetMapping("/sendMail")
public String sendMail(){if (msg.isEmpty()){String msg ="无告警信息";}sendMailUtils.sendText("Cpu使用率",msg," xxxx@qq.com","xxxx@qq.com");return "ok";
}@GetMapping("/sendMails")
public String sendMails(){try {sendMailUtils.sendTexts("发送带页面格式加文件邮件测试","<p style='color:red;'>红</p>",true, " xxxx@qq.com"," xxxx@qq.com","redis图标","C:\\Users\\Administrator\\Desktop\\redis.png");} catch (MessagingException e) {throw new RuntimeException(e);}return "ok";
}
发送效果