maven依赖
<!--邮箱验证-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
yml文件配置
spring:
mail: #邮箱
default-encoding: UTF-8
host: smtp.qq.com
username: 你的QQ邮箱@qq.com #发送邮件的邮箱
password: xxxxxxxxxxx #发送邮件邮箱的KEY,可以去百度一下邮箱如何开启stmp 就知道了
port: 465
auth: true
properties:
mail.smtp.socketFactory.fallback: false
mail.smtp.socketFactory.port: 465
mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory
邮件发送实体
package com.ruoyi.email.entity;
import lombok.Data;
@Data
public class EmailEntity {
private String sendTo;
private String title;
private String detail;
private String fileList;
}
请求响应对象AjaxResult类
package com.ruoyi.common.core.domain;
import cn.hutool.http.HttpStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel("请求响应对象")
public class AjaxResult<T> {
private static final long serialVersionUID = 1L;
@ApiModelProperty("消息状态码")
private int code;
@ApiModelProperty("消息内容")
private String msg;
@ApiModelProperty("数据对象")
private T data;
public AjaxResult(int code, String msg) {
this.code = code;
this.msg = msg;
}
public static AjaxResult<Void> success() {
return AjaxResult.success("操作成功");
}
public static <T> AjaxResult<T> success(T data) {
return AjaxResult.success("操作成功", data);
}
public static AjaxResult<Void> success(String msg) {
return AjaxResult.success(msg, null);
}
public static <T> AjaxResult<T> success(String msg, T data) {
return new AjaxResult<>(HttpStatus.HTTP_OK, msg, data);
}
public static AjaxResult<Void> error() {
return AjaxResult.error("操作失败");
}
public static AjaxResult<Void> error(String msg) {
return AjaxResult.error(msg, null);
}
public static <T> AjaxResult<T> error(String msg, T data) {
return new AjaxResult<>(HttpStatus.HTTP_INTERNAL_ERROR, msg, data);
}
public static AjaxResult<Void> error(int code, String msg) {
return new AjaxResult<>(code, msg, null);
}
}
邮件发送业务类EmailService
package com.ruoyi.email.service;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.CommonUtil;
import com.ruoyi.email.entity.EmailEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory