先说smtp邮箱服务配置
maven-pom依赖配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application.yml
spring:
mail:
#配置smtp服务主机地址
# qq邮箱为smtp.qq.com 端口号465或587
# sina smtp.sina.cn
# aliyun smtp.aliyun.com
# 163 smtp.163.com 端口号465或994
# 腾讯企业邮|域名邮箱 smtp.exmail.qq.com 端口号465
host: 【自行选择】
#发送者邮箱
username: 【自行修改】
#配置密码,注意不是真正的密码,而是刚刚申请到的授权码
password: 【自行修改】
#端口号465或587
port: 465
#默认的邮件编码为UTF-8
default-encoding: UTF-8
#其他参数
test-connection: true
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
ssl:
enable: true
socketFactory:
port: 465
class: javax.net.ssl.SSLSocketFactory
配置包装类用于读取SMTP配置
/**
* Title
*
* @ClassName: MailCommon
* @Description: 读取yml-mail信息
* @author: Karos
* @date: 2022/10/14 1:00
* @Blog: https://www.wzl1.top/
*/
package com.karos.td.common;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//所有变量不能设置为静态变量
@Component
@ToString
public class EmailSettingCommon {
@Value("${spring.mail.username}")
public String username;
@Value("${spring.mail.host}")
public String host;
@Value("${spring.mail.password}")
public String password;
@Value("${spring.mail.port}")
public int port;
@Value("${spring.mail.default-encoding}")
public String default_encoding;
}
Bean构造类
/**
* Title
*
* @ClassName: getBean
* @Description:SimpleMailMessage和mail配置自动装配
* @author: Karos
* @date: 2022/10/15 2:54
* @Blog: https://www.wzl1.top/
*/
package com.karos.td.common;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Component;
@Component
public class getBean {
@Bean
public static SimpleMailMessage smm(){
return new SimpleMailMessage();
}
// @Bean
// public static EmailSettingCommon getInstance(){
// return new EmailSettingCommon();
// }
}
配置邮箱服务类
/**
* Title
*
* @ClassName: EmailSend
* @Description:
* @author: Karos
* @date: 2022/10/15 2:55
* @Blog: https://www.wzl1.top/
*/
package com.karos.td.Util;
import com.karos.td.common.EmailSettingCommon;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
@Log4j2
public class EmailSend {
@Autowired
private JavaMailSenderImpl impl;
@Autowired
private SimpleMailMessage smm;
@Autowired
private EmailSettingCommon esc;
public void setMessage(String toMail,String Subject,String Text){
// if (impl.getPassword()==null)SmmInit(); //如果无法读取到yml配置文件可以试一试这个,Spring自动装配@Autowired不建议使用与静态对象,@Value只有在自动装配的时候才会写入
smm.setFrom(esc.username);
smm.setTo(toMail);
smm.setSubject(Subject);
smm.setText(Text);
smm.setSentDate(new Date());
log.info(impl.getPassword());
}
// private void SmmInit(){
// impl.setPassword(esc.password);
// impl.setUsername(esc.username);
// impl.setHost(esc.host);
// impl.setDefaultEncoding(esc.default_encoding);
// impl.setPort(esc.port);
// }
public void send(){
impl.send(smm);
}
}
验证码生成与密码加密类
/**
* Title
*
* @ClassName: UserSafetyUtil
* @Description: 用户安全工具,生成验证码,密码加密等
* @author: Karos
* @date: 2022/10/15 9:32
* @Blog: https://www.wzl1.top/
*/
package com.karos.td.Util;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import java.util.Date;
import java.util.Locale;
@Component
public class UserSafetyUtil {
public static String StrHex(StringBuffer str,StringBuffer key){
str.append(key);
str.insert(0,getMD5Hex(key.toString()));
return getMD5Hex(str.insert(5,"wzl03").toString());
}
//这个验证码获取我是瞎写的,主要还是靠时间戳来达到一个随机的效果
public static String touchCheckCode(String str,String key){
String s=StrHex(new StringBuffer(str),new StringBuffer(key));
return getMD5Hex(s.substring(5,10)).substring(5,11).toUpperCase(Locale.ROOT);
}
public static String getMD5Hex(String str){
return DigestUtils.md5DigestAsHex(str.getBytes());
}
public static String getTIMEstr() {
return new Long((new Date().getTime()>>3|1024|8086)%3000).toString();
}
}
写一个@Test
package com.karos.td;
import com.karos.td.Util.EmailSend;
import com.karos.td.Util.UserSafetyUtil;
import lombok.extern.log4j.Log4j2;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@Log4j2
class TdApplicationTests {
@Autowired
EmailSend ems;
@Test
void contextLoads() {
}
@Test
void mailSend(){
log.info("====邮件发送测试开始====");
ems.setMessage("[email protected]","测试","验证码为:"+ UserSafetyUtil.touchCheckCode("admin", UserSafetyUtil.getTIMEstr()));
log.info("====邮件设定成功====");
ems.send();
log.info("====邮件发送成功====");
}
}
后面的话Redis存储验证码和JJWT登录验证以后更新
正文完