Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Optimize email service #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions api/src/main/java/cn/crap/service/tool/EmailService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package cn.crap.service.tool;

import cn.crap.dto.MailBean;
import cn.crap.enu.SettingEnum;
import cn.crap.service.IEmailService;
import cn.crap.beans.Config;
import cn.crap.utils.Aes;
import cn.crap.utils.IConst;
import cn.crap.utils.ISetting;
Expand All @@ -18,6 +16,7 @@
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.Objects;

@Service
public class EmailService implements IEmailService {
Expand All @@ -35,7 +34,7 @@ public void sendMail(MailBean mailBean) throws UnsupportedEncodingException, Mes
String fromName = settingCache.get(ISetting.S_TITLE).getValue();
MimeMessage mimeMessage = mailSenderService.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
messageHelper.setFrom(mailSenderService.getUsername(), fromName);
messageHelper.setFrom(Objects.requireNonNull(mailSenderService.getUsername()), fromName);
messageHelper.setSubject(mailBean.getSubject());
messageHelper.setTo(mailBean.getToEmail());
messageHelper.setText(mailBean.getContext(), true);// html: true
Expand All @@ -49,7 +48,7 @@ public boolean sendMail(String subject, String toEmail, String context) {
MimeMessage mimeMessage = mailSenderService.createMimeMessage();
MimeMessageHelper messageHelper;
messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
messageHelper.setFrom(mailSenderService.getUsername(), fromName);
messageHelper.setFrom(Objects.requireNonNull(mailSenderService.getUsername()), fromName);
messageHelper.setSubject(subject);
messageHelper.setTo(toEmail);
messageHelper.setText(context, true);// html: true
Expand All @@ -63,41 +62,38 @@ public boolean sendMail(String subject, String toEmail, String context) {
}

@Override
public void sendRegisterEmail(String eamil, String id) throws UnsupportedEncodingException, MessagingException{
public void sendRegisterEmail(String email, String id) throws UnsupportedEncodingException, MessagingException{
String code = Aes.encrypt(id);
String domain = Tools.getUrlPath() + "/user/validateEmail.do?i=" + code;
MailBean mailBean = new MailBean();
mailBean.setContext( getMtml(eamil, "注册邮箱验证", "点击验证邮箱:<a href=\""+domain+"\">"+domain+"</a>"));
mailBean.setToEmail(eamil);
mailBean.setContext( getMtml(email, "注册邮箱验证", "点击验证邮箱:<a href=\""+domain+"\">"+domain+"</a>"));
mailBean.setToEmail(email);
mailBean.setSubject("注册邮箱验证-开源API接口管理系统");
sendMail(mailBean);
logger.warn(mailBean.getContext());
stringCache.add(code, IConst.REGISTER);
}

@Override
public void sendFindPwdEmail(String eamil) throws UnsupportedEncodingException, MessagingException{
public void sendFindPwdEmail(String email) throws UnsupportedEncodingException, MessagingException{
MailBean mailBean = new MailBean();
String code = Tools.getChar(6);
mailBean.setContext( getMtml(eamil, "找回密码", "邮件验证码为:"+code));
mailBean.setToEmail(eamil);
mailBean.setContext( getMtml(email, "找回密码", "邮件验证码为:"+code));
mailBean.setToEmail(email);
mailBean.setSubject("找回密码-开源API接口管理系统");
sendMail(mailBean);
stringCache.add(IConst.CACHE_FINDPWD+ eamil, code);
stringCache.add(IConst.CACHE_FINDPWD+ email, code);
}

private String getMtml(String eamil, String title, String content){
StringBuffer sb = new StringBuffer();
sb.append("<div style=\"position:relative;width:400px;margin:0 auto; background:#f7f7f7;color:#999999; font-size:14px;line-height:36px;\">");
sb.append("<div style=\"height:60px; border-bottom:2px solid #f82c1d;padding:10px;\" >");
sb.append("<div style=\"float:left;margin-left:10px; line-height:60px;font-size:18px;font-weight:bold;color:#555;width:360px;height:60px;overflow:hidden;text-align:left;\">");
sb.append( title );
sb.append("</div></div><div style=\"padding:20px;min-height:260px;white-space: pre-wrap;word-wrap: break-word;\">");
sb.append(content);
sb.append("</div><div style=\"padding:20px;text-align:right;margin-top:30px;\">");
sb.append("<a style=\"color:#f82c1d;\" href=\"http://api.crap.cn?sj="
+ System.currentTimeMillis() + "\">本网站由CrapApi提供技术与支持</a>");
sb.append("<br></div></div>");
return sb.toString();
private String getMtml(String email, String title, String content){
return "<div style=\"position:relative;width:400px;margin:0 auto; background:#f7f7f7;color:#999999; font-size:14px;line-height:36px;\">" +
"<div style=\"height:60px; border-bottom:2px solid #f82c1d;padding:10px;\" >" +
"<div style=\"float:left;margin-left:10px; line-height:60px;font-size:18px;font-weight:bold;color:#555;width:360px;height:60px;overflow:hidden;text-align:left;\">" +
title +
"</div></div><div style=\"padding:20px;min-height:260px;white-space: pre-wrap;word-wrap: break-word;\">" +
content +
"</div><div style=\"padding:20px;text-align:right;margin-top:30px;\">" +
"<a style=\"color:#f82c1d;\" href=\"http://api.crap.cn?sj=" + System.currentTimeMillis() + "\">本网站由CrapApi提供技术与支持</a>" +
"<br></div></div>";
}
}