update 在离线和验证码邮件发送改为html
This commit is contained in:
@@ -59,6 +59,18 @@
|
||||
<artifactId>common-security</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Thymeleaf 用于发送邮箱 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
<version>1.5.0.RC1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -6,12 +6,11 @@ import com.m2pool.auth.service.impl.MaliServiceImpl;
|
||||
import com.m2pool.common.core.Result.R;
|
||||
import com.m2pool.common.core.utils.JwtUtils;
|
||||
import com.m2pool.common.core.utils.StringUtils;
|
||||
import com.m2pool.common.core.web.Result.AjaxResult;
|
||||
import com.m2pool.common.security.annotation.RequiresLogin;
|
||||
import com.m2pool.common.security.auth.AuthUtil;
|
||||
import com.m2pool.common.security.service.TokenService;
|
||||
import com.m2pool.common.security.utils.SecurityUtils;
|
||||
import com.m2pool.system.api.entity.SysUser;
|
||||
import com.m2pool.system.api.entity.EmailTemplateEntity;
|
||||
import com.m2pool.system.api.model.LoginUser;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
@@ -163,4 +162,13 @@ public class TokenController {
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("sendHtmlMailMessage")
|
||||
public R<?> sendHtmlMailMessage(@Valid @RequestBody EmailTemplateEntity entity)
|
||||
{
|
||||
maliService.sendHtmlMailMessage(entity);
|
||||
return R.success("邮件已发送");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.m2pool.auth.service;
|
||||
import com.m2pool.auth.entity.GetEmailCodeEntity;
|
||||
import com.m2pool.auth.entity.GetLoginEmailCodeEntity;
|
||||
import com.m2pool.common.core.Result.R;
|
||||
import com.m2pool.system.api.entity.EmailTemplateEntity;
|
||||
|
||||
/**
|
||||
* @Description TODO
|
||||
@@ -19,12 +20,9 @@ public interface MailService {
|
||||
public void sendTextMailMessage(String to,String subject,String text);
|
||||
|
||||
/**
|
||||
* 发送html邮件
|
||||
* @param to
|
||||
* @param subject
|
||||
* @param content
|
||||
* 发送矿机离线数 html邮件
|
||||
*/
|
||||
public void sendHtmlMailMessage(String to,String subject,String content);
|
||||
public void sendHtmlMailMessage(EmailTemplateEntity entity);
|
||||
|
||||
/**
|
||||
* 发送带附件的邮件
|
||||
|
||||
@@ -8,12 +8,12 @@ import com.m2pool.auth.service.MailService;
|
||||
import com.m2pool.common.core.RedisTransKey;
|
||||
import com.m2pool.common.core.Result.R;
|
||||
import com.m2pool.common.core.constant.SecurityConstants;
|
||||
import com.m2pool.common.core.exception.ServiceException;
|
||||
import com.m2pool.common.core.utils.CodeUtils;
|
||||
import com.m2pool.common.core.utils.StringUtils;
|
||||
import com.m2pool.common.redis.service.RedisService;
|
||||
import com.m2pool.common.security.utils.SecurityUtils;
|
||||
import com.m2pool.system.api.RemoteUserService;
|
||||
import com.m2pool.system.api.entity.EmailTemplateEntity;
|
||||
import com.m2pool.system.api.model.LoginUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -21,9 +21,15 @@ import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.thymeleaf.TemplateEngine;
|
||||
import org.thymeleaf.context.Context;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@@ -42,12 +48,19 @@ public class MaliServiceImpl implements MailService {
|
||||
@Autowired
|
||||
private RemoteUserService remoteUserService;
|
||||
|
||||
@Autowired
|
||||
private TemplateEngine templateEngine;
|
||||
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
@Value("${spring.mail.username}")
|
||||
private String sendMailer;
|
||||
|
||||
|
||||
@Value("${image.prefix}")
|
||||
private String imagePrefix;
|
||||
|
||||
public static String EMAIL_REGEX="^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
|
||||
|
||||
/**
|
||||
@@ -103,42 +116,52 @@ public class MaliServiceImpl implements MailService {
|
||||
|
||||
|
||||
/**
|
||||
* 发送html邮件
|
||||
* 发送HTML邮件
|
||||
* @param to
|
||||
* @param subject
|
||||
* @param content
|
||||
* @param templateName 模版名
|
||||
* @param variables 需要映射到html上的动态内容
|
||||
* @throws MessagingException
|
||||
*/
|
||||
public void sendHtmlEmail(String to, String subject, String templateName, Map<String, Object> variables) throws MessagingException {
|
||||
// 创建 MimeMessage 对象
|
||||
MimeMessage message = javaMailSender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(message, true);
|
||||
|
||||
|
||||
//邮件发信人
|
||||
helper.setFrom(sendMailer);
|
||||
|
||||
// 收件人一个活多个
|
||||
helper.setTo(to.split(","));
|
||||
helper.setSubject(subject);
|
||||
|
||||
// 创建 Thymeleaf 上下文并添加变量
|
||||
Context context = new Context();
|
||||
//设置图片访问前缀
|
||||
variables.put("imagePrefix", imagePrefix);
|
||||
context.setVariables(variables);
|
||||
|
||||
// 处理 HTML 模板
|
||||
String htmlContent = templateEngine.process(templateName, context);
|
||||
|
||||
// 设置邮件内容为 HTML
|
||||
helper.setText(htmlContent, true);
|
||||
|
||||
//邮件发送时间
|
||||
helper.setSentDate(new Date());
|
||||
|
||||
// 发送邮件
|
||||
javaMailSender.send(message);
|
||||
System.out.println("发送邮件成功:"+sendMailer+"->"+to);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void sendHtmlMailMessage(String to,String subject,String content){
|
||||
|
||||
content="<!DOCTYPE html>\n" +
|
||||
"<html>\n" +
|
||||
"<head>\n" +
|
||||
"<meta charset=\"utf-8\">\n" +
|
||||
"<title>邮件</title>\n" +
|
||||
"</head>\n" +
|
||||
"<body>\n" +
|
||||
"\t<h3>这是一封HTML邮件!</h3>\n" +
|
||||
"</body>\n" +
|
||||
"</html>";
|
||||
public void sendHtmlMailMessage(EmailTemplateEntity entity){
|
||||
try {
|
||||
//true 代表支持复杂的类型
|
||||
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(),true);
|
||||
//邮件发信人
|
||||
mimeMessageHelper.setFrom(sendMailer);
|
||||
//邮件收信人 1或多个
|
||||
mimeMessageHelper.setTo(to.split(","));
|
||||
//邮件主题
|
||||
mimeMessageHelper.setSubject(subject);
|
||||
//邮件内容 true 代表支持html
|
||||
mimeMessageHelper.setText(content,true);
|
||||
//邮件发送时间
|
||||
mimeMessageHelper.setSentDate(new Date());
|
||||
|
||||
//发送邮件
|
||||
javaMailSender.send(mimeMessageHelper.getMimeMessage());
|
||||
System.out.println("发送邮件成功:"+sendMailer+"->"+to);
|
||||
|
||||
sendHtmlEmail(entity.getEmail(),entity.getSubject(),entity.getTemplateName(),entity.getData());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("发送邮件失败:"+e.getMessage());
|
||||
@@ -189,9 +212,13 @@ public class MaliServiceImpl implements MailService {
|
||||
*/
|
||||
@Override
|
||||
public void sendCodeMailMessage(String to, String code) {
|
||||
String subject = "账号注册,邮箱验证码";
|
||||
String text = "注册验证码10分钟内有效:\n\t"+code;
|
||||
sendTextMailMessage(to,subject,text);
|
||||
//String subject = "账号注册,邮箱验证码";
|
||||
//String text = "注册验证码10分钟内有效:\n\t"+code;
|
||||
Map<String, Object> content = new HashMap<>();
|
||||
content.put("code",code);
|
||||
content.put("text","The verification code for registration is valid for 10 minutes");
|
||||
EmailTemplateEntity message = new EmailTemplateEntity(to,"Account registration, email verification code","emailCode-en",content);
|
||||
sendHtmlMailMessage(message);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -201,25 +228,39 @@ public class MaliServiceImpl implements MailService {
|
||||
*/
|
||||
@Override
|
||||
public void sendLoginCodeMailMessage(String to, String code) {
|
||||
String subject = "用户登录,邮箱验证码";
|
||||
String text = "登录验证码10分钟内有效:\n\t"+code;
|
||||
sendTextMailMessage(to,subject,text);
|
||||
//String subject = "用户登录,邮箱验证码";
|
||||
//String text = "登录验证码10分钟内有效:\n\t"+code;
|
||||
Map<String, Object> content = new HashMap<>();
|
||||
content.put("code",code);
|
||||
content.put("text","Login verification code is valid within 10 minutes");
|
||||
EmailTemplateEntity entity = new EmailTemplateEntity(to,"User login, email verification code","emailCode-en",content);
|
||||
sendHtmlMailMessage(entity);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void sendResetPwdMailMessage(String to, String code) {
|
||||
String subject = "账号重置密码,邮箱验证码";
|
||||
String text = "您正在重置密码,如果不是您本人操作,请忽略。验证码10分钟内有效:\n\t"+code;
|
||||
sendTextMailMessage(to,subject,text);
|
||||
//String subject = "账号重置密码,邮箱验证码";
|
||||
//String text = "您正在重置密码,如果不是您本人操作,请忽略。验证码10分钟内有效:\n\t"+code;
|
||||
|
||||
Map<String, Object> content = new HashMap<>();
|
||||
content.put("code",code);
|
||||
content.put("text","You are resetting your password. If this is not done by you, please ignore it. The verification code is valid for 10 minutes.");
|
||||
EmailTemplateEntity entity = new EmailTemplateEntity(to,"Reset account password, email verification code","emailCode-en",content);
|
||||
sendHtmlMailMessage(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendUpdatePwdMailMessage(String to, String code) {
|
||||
String subject = "修改密码,邮箱验证码";
|
||||
String text = "您正在修改密码,如果不是您本人操作,请忽略。验证码10分钟内有效:\n\t"+code;
|
||||
sendTextMailMessage(to,subject,text);
|
||||
//String subject = "修改密码,邮箱验证码";
|
||||
//String text = "您正在修改密码,如果不是您本人操作,请忽略。验证码10分钟内有效:\n\t"+code;
|
||||
|
||||
Map<String, Object> content = new HashMap<>();
|
||||
content.put("code",code);
|
||||
content.put("text","You are currently modifying your password. If this is not done by you, please ignore it. The verification code is valid for 10 minutes.");
|
||||
EmailTemplateEntity entity = new EmailTemplateEntity(to,"Change password, email verification code","emailCode-en",content);
|
||||
sendHtmlMailMessage(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -62,3 +62,5 @@ spring:
|
||||
namespace: m2_prod
|
||||
group: m2_prod_group
|
||||
|
||||
image:
|
||||
prefix: https://m2pool.com
|
||||
|
||||
@@ -87,3 +87,5 @@ spring:
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
namespace: m2_test
|
||||
group: m2_test_group
|
||||
image:
|
||||
prefix: https://test.m2pool.com
|
||||
|
||||
Reference in New Issue
Block a user