邮箱激活码的发送

This commit is contained in:
Zhang Liguo 2023-12-19 21:48:38 +08:00
parent 8677b21928
commit 0631af1ac5
13 changed files with 529 additions and 0 deletions

23
pom.xml
View File

@ -48,6 +48,28 @@
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!--邮箱依赖配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--解析HTML将其转成字符串 -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
@ -59,6 +81,7 @@
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>

View File

@ -0,0 +1,4 @@
package com.lovenav.controller;
public class UserController {
}

View File

@ -49,6 +49,86 @@ public class User implements Serializable {
*/
private Byte userStatus;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserLogin() {
return userLogin;
}
public void setUserLogin(String userLogin) {
this.userLogin = userLogin;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Long getPhone() {
return phone;
}
public void setPhone(Long phone) {
this.phone = phone;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public Byte getUserStatus() {
return userStatus;
}
public void setUserStatus(Byte userStatus) {
this.userStatus = userStatus;
}
public Date getUserRegistered() {
return userRegistered;
}
public void setUserRegistered(Date userRegistered) {
this.userRegistered = userRegistered;
}
public Byte getRoleId() {
return roleId;
}
public void setRoleId(Byte roleId) {
this.roleId = roleId;
}
/**
* 用户注册时间
*/

View File

@ -16,4 +16,7 @@ public interface UserDao {
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}

View File

@ -0,0 +1,9 @@
package com.lovenav.service;
import com.lovenav.entity.User;
public interface UserService {
public void sendEmailActivecode(User user,String activecode);
}

View File

@ -0,0 +1,14 @@
package com.lovenav.service.serviceImpl;
import com.lovenav.utls.EmailUtils;
import com.lovenav.entity.User;
import com.lovenav.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public void sendEmailActivecode(User user,String activecode) {
EmailUtils.sendEmail(user,activecode);
}
}

View File

@ -0,0 +1,100 @@
package com.lovenav.utls;
import com.lovenav.entity.User;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
import java.util.Properties;
/*
* 基于JDK实现邮件发送
* 主要是实现激活码的发送
* */
public class EmailUtils {
public static void sendEmail(User user,String activecode) {
//邮箱 lx_teach@163.com
String myAccount = "482370576@qq.com";
//授权码 java168
String myPass = "rgggscegbprzbhcc";
//邮箱服务器
String SMTPHost = "smtp.qq.com";
//设置属性信息
Properties prop = new Properties();
//设置协议
prop.setProperty("mail.transport.protocol", "smtp");
//邮件服务器
prop.setProperty("mail.smtp.host", SMTPHost);
//认证
prop.setProperty("mail.smtp.auth", "true");
//1创建会话
Session session = Session.getDefaultInstance(prop);
//设置是否需要调试
session.setDebug(false);
//2创建发送信息
MimeMessage message = createMsg(session, myAccount, user, activecode);
//4发送信息操作
try {
Transport tran = session.getTransport();
//连接
tran.connect(myAccount, myPass);
//发送消息
tran.sendMessage(message, message.getAllRecipients());
//关闭
tran.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//生成邮件消息
private static MimeMessage createMsg(Session session, String myAccount, User user,String activecode) {
//创建消息对象
MimeMessage message = new MimeMessage(session);
//设置
try {
//3.1发送方
message.setFrom(new InternetAddress(myAccount, "官方邮件", "utf-8"));
//3.2设置接收方
/*
* MimeMessage.RecipientType.TO
* MimeMessage.RecipientType.CC
* MimeMessage.RecipientType.BCC
* */
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(user.getUserEmail()));
//3.3 设置主题
message.setSubject("激活码", "utf-8");
//获取本机的ip地址
// String ip = Inet4Address.getLocalHost().getHostAddress();
// String url = "http://" + ip + ":80/activate?e=" + Base64Utils.encode(user.getEmail()) + "&c=" + Base64Utils.encode(user.getActivatecode());
//设置正文信息
// message.setContent(user.getUsername() + ",欢迎你加入我们<br>为了更好体验我们的产品,请<a href='" + url + "'>点击激活 " + url + "</a>", "text/html;charset=utf-8");
File file=new File("src/main/resources/static/sendEmailActivecode.html");
Document document= Jsoup.parse(file,"utf-8");
document.getElementById("0").getElementsByClass("button").append(String.valueOf(activecode.charAt(0)));
document.getElementById("1").getElementsByClass("button").append(String.valueOf(activecode.charAt(1)));
document.getElementById("2").getElementsByClass("button").append(String.valueOf(activecode.charAt(2)));
document.getElementById("3").getElementsByClass("button").append(String.valueOf(activecode.charAt(3)));
message.setContent( document.toString(), "text/html;charset=utf-8");
//设置日期
message.setSentDate(new Date());
//保存
message.saveChanges();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return message;
}
}

View File

@ -0,0 +1,41 @@
package com.lovenav.utls;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
* @Title: FileUtils.java
* @Package com.qfedu.common.utils
* @Description: TODO(用一句话描述该文件做什么)
* @author Feri
* @date 2018年5月29日
* @version V1.0
* 文件工具类
*/
public class FileUtils {
//创建文件夹 一个月一个文件夹
public static File createDir(String dir) {
//子文件名称201805 201806
String month = new SimpleDateFormat("yyyyMM").format(new Date());
File dir1 = new File(new File(dir).getParent(), "fmwimages");
File dir2 = new File(dir1, month);
if (!dir2.exists()) {
dir2.mkdirs();
}
return dir2;
}
//创建唯一名称
public static String createFileName(String fn) {
if (fn.length() > 30) {
fn = fn.substring(fn.length() - 30);
}
return UUID.randomUUID().toString() + "_" + fn;
}
}

View File

@ -0,0 +1,23 @@
package com.lovenav.utls;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//MD5 摘要算法 签名或者简易加解密
public class MD5Utils {
public static String md5(String password) {
try {
//获取摘要对象
MessageDigest md = MessageDigest.getInstance("MD5");
//设置要签名的内容
md.update(password.getBytes());
//获取摘要结果
return new BigInteger(1, md.digest()).toString(16);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}

View File

@ -0,0 +1,41 @@
package com.lovenav.utls;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
import java.util.UUID;
//随机数
public class RandomUtils {
//生成激活码
public static String createActive() {
// 生成4位随机数字+字母,
String val = "";
Random random = new Random();
// 参数length表示生成几位随机数
for (int i = 0; i < 4; i++) {
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
// 输出字母还是数字
if ("char".equalsIgnoreCase(charOrNum)) {
// 输出是大写字母还是小写字母
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char) (random.nextInt(26) + temp);
} else if ("num".equalsIgnoreCase(charOrNum)) {
val += String.valueOf(random.nextInt(10));
}
}
return val;
}
//设置时间戳
public static String getTime() {
return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(Calendar.getInstance().getTime());
}
//生成订单编号
public static String createOrderId() {
return getTime() + UUID.randomUUID().toString();
}
}

View File

@ -0,0 +1,25 @@
package com.lovenav.utls;
import com.lovenav.entity.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpSession;
public class SessionUtils {
private static final String USERKEY = "sessionUser";
public static HttpSession session() {
ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
return attr.getRequest().getSession(true); // true == allow create
}
public static User getCurrentUserInfo() {
return (User) session().getAttribute(USERKEY);
}
public static void saveCurrentUserInfo(User User) {
session().setAttribute(USERKEY, User);
}
}

View File

@ -0,0 +1,18 @@
package com.lovenav.utls;
//字符串的各种校验
public class StrUtils {
/**
* 是否为空的校验
* @return true false 非空*/
public static boolean empty(String...msg){
boolean res = false;
for(String s:msg){
res = (s==null|| s.length()==0);
if(res) {
break;
}
}
return res;
}
}

View File

@ -0,0 +1,148 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>邮箱验证码</title>
<style>
table {
width: 700px;
margin: 0 auto;
}
#top {
width: 700px;
border-bottom: 1px solid #ccc;
margin: 0 auto 30px;
}
#top table {
font: 12px Tahoma, Arial, 宋体;
height: 40px;
}
#content {
width: 680px;
padding: 0 10px;
margin: 0 auto;
}
#content_top {
line-height: 1.5;
font-size: 14px;
margin-bottom: 25px;
color: #4d4d4d;
}
#content_top strong {
display: block;
margin-bottom: 15px;
}
#content_top strong span {
color: #f60;
font-size: 16px;
}
#verificationCode {
color: #f60;
font-size: 24px;
}
#content_bottom {
margin-bottom: 30px;
}
#content_bottom small {
display: block;
margin-bottom: 20px;
font-size: 12px;
color: #747474;
}
#bottom {
width: 700px;
margin: 0 auto;
}
#bottom div {
padding: 10px 10px 0;
border-top: 1px solid #ccc;
color: #747474;
margin-bottom: 20px;
line-height: 1.3em;
font-size: 12px;
}
#content_top strong span {
font-size: 18px;
color: #FE4F70;
}
#sign {
text-align: right;
font-size: 18px;
color: #FE4F70;
font-weight: bold;
}
#verificationCode {
height: 100px;
width: 680px;
text-align: center;
margin: 30px 0;
}
#verificationCode div {
height: 100px;
width: 680px;
}
.button {
color: #FE4F70;
margin-left: 10px;
height: 80px;
width: 80px;
resize: none;
font-size: 42px;
border: none;
outline: none;
padding: 10px 15px;
background: #ededed;
text-align: center;
border-radius: 17px;
box-shadow: 6px 6px 12px #cccccc,
-6px -6px 12px #ffffff;
}
.button:hover {
box-shadow: inset 6px 6px 4px #d1d1d1,
inset -6px -6px 4px #ffffff;
}
</style>
</head>
<body>
<table>
<tbody>
<tr>
<td>
<div id="top">
<table>
<tbody><tr><td></td></tr></tbody>
</table>
</div>
<div id="content">
<div id="content_top">
<strong>尊敬的用户:您好!</strong>
<strong>
您正在进行<span>注册账号</span>操作,请在验证码中输入以下验证码完成操作:
</strong>
<div id="verificationCode">
<button id="0" class="button"></button>
<button id="1" class="button"></button>
<button id="2" class="button"></button>
<button id="3" class="button"></button>
</div>
</div>
<div id="content_bottom">
<small>
注意:此操作可能会修改您的密码、登录邮箱或绑定手机。如非本人操作,请及时登录并修改密码以保证帐户安全
<br>(工作人员不会向你索取此验证码,请勿泄漏!)
</small>
</div>
</div>
<div id="bottom">
<div>
<p>此为系统邮件,请勿回复<br>
请保管好您的邮箱,避免账号被他人盗用
</p>
<p id="sign">——LoveNav</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</body>