发送邮件配置邮件模板
This commit is contained in:
14
src/main/java/com/quinn/common/EmailType.java
Normal file
14
src/main/java/com/quinn/common/EmailType.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.quinn.common;
|
||||
|
||||
public enum EmailType {
|
||||
|
||||
/**
|
||||
* 论坛
|
||||
*/
|
||||
REGISTER,
|
||||
/**
|
||||
* 资源
|
||||
*/
|
||||
SUCCESS
|
||||
|
||||
}
|
||||
@@ -35,4 +35,6 @@ public interface QuinnConstant {
|
||||
String DEFAULT_ATTR_BASE64 = "/images/avatar/a1.png";
|
||||
|
||||
String DEFAULT_ATTR = "/images/avatar/a#.png";
|
||||
|
||||
String EMAIL_REPLACE = "#!!#";
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
// 访问权限
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/","/index","/favicon.ico").permitAll()
|
||||
.antMatchers("/register","/login","/toLogin","/checkLogin").permitAll()
|
||||
.antMatchers("/register","/login","/toLogin","/checkLogin","/checkRegister").permitAll()
|
||||
.antMatchers("/source","/source/view/*").permitAll()
|
||||
.antMatchers("/blog","/blog/read/*").permitAll()
|
||||
.antMatchers("/search/**").permitAll()
|
||||
|
||||
@@ -79,20 +79,27 @@ public class LoginController {
|
||||
// 表单密码重复判断
|
||||
if (!registerForm.getPassword().equals(registerForm.getRepassword())){
|
||||
toResult(writer,"两次输入密码不一致!");
|
||||
return;
|
||||
}
|
||||
// 用户名已存在
|
||||
User hasUser = userService.getOne(new QueryWrapper<User>().eq("username", registerForm.getUsername()));
|
||||
if (hasUser!=null){
|
||||
toResult(writer,"用户名太热门了,请更换用户名");
|
||||
return;
|
||||
}
|
||||
// 验证邀请码
|
||||
Invite invite = inviteService.getOne(new QueryWrapper<Invite>().eq("code", registerForm.getCode()));
|
||||
Invite invite = inviteService.getOne(new QueryWrapper<Invite>()
|
||||
.eq("code", registerForm.getCode())
|
||||
.eq("status",'0'));
|
||||
if (invite==null){
|
||||
toResult(writer,"注册码不存在!");
|
||||
return;
|
||||
}
|
||||
if (!invite.getApplyEmail().equals(registerForm.getEmail())){
|
||||
toResult(writer,"请使用申请注册码邮箱注册用户!");
|
||||
return;
|
||||
}
|
||||
toResult(writer,"ok");
|
||||
}
|
||||
|
||||
// 注册业务
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.quinn.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.quinn.common.EmailType;
|
||||
import com.quinn.dto.req.SearchPage;
|
||||
import com.quinn.dto.req.SendCode;
|
||||
import com.quinn.dto.res.AboutDTO;
|
||||
@@ -10,23 +11,15 @@ import com.quinn.dto.res.ResponseDTO;
|
||||
import com.quinn.intergration.SendBMail;
|
||||
import com.quinn.pojo.About;
|
||||
import com.quinn.pojo.Invite;
|
||||
import com.quinn.pojo.UserInfo;
|
||||
import com.quinn.service.AboutService;
|
||||
import com.quinn.service.InviteService;
|
||||
import com.quinn.service.UserInfoService;
|
||||
import com.quinn.service.UserService;
|
||||
import com.quinn.utils.QuinnUtils;
|
||||
import com.quinn.vo.MyPageParam;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -44,9 +37,9 @@ public class WxController extends BaseModelController {
|
||||
@Resource
|
||||
AboutService aboutService;
|
||||
@Resource
|
||||
UserInfoService userInfoService;
|
||||
@Resource
|
||||
InviteService inviteService;
|
||||
@Resource
|
||||
SendBMail sendBMail;
|
||||
|
||||
@PostMapping("about/us")
|
||||
public ResponseDTO userIndexBlog(SearchPage searchPage){
|
||||
@@ -83,9 +76,9 @@ public class WxController extends BaseModelController {
|
||||
invite.setCode(code);
|
||||
invite.setGmtCreate(QuinnUtils.getTime());
|
||||
inviteService.save(invite);
|
||||
boolean sendFlag = SendBMail.INSTANCE.sendRegisterMail(code, sendCode.getEmail());
|
||||
boolean sendFlag = sendBMail.sendOneParamMail(EmailType.REGISTER,code, sendCode.getEmail());
|
||||
if (!sendFlag){
|
||||
return ResponseDTO.ok().setData("发送识别,请确认邮箱是否正确!");
|
||||
return ResponseDTO.ok().setData("发送失败,请确认邮箱是否正确!");
|
||||
}
|
||||
return ResponseDTO.ok().setData("success");
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public class CodeGenerator {
|
||||
|
||||
// 5、策略配置
|
||||
StrategyConfig strategy = new StrategyConfig();
|
||||
strategy.setInclude("qn_star");//设置要映射的表名
|
||||
strategy.setInclude("qn_email");//设置要映射的表名
|
||||
strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
|
||||
strategy.setTablePrefix("qn_");//设置表前缀不生成
|
||||
|
||||
|
||||
@@ -1,7 +1,16 @@
|
||||
package com.quinn.intergration;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.quinn.common.EmailType;
|
||||
import com.quinn.common.QuinnConstant;
|
||||
import com.quinn.pojo.Email;
|
||||
import com.quinn.service.EmailService;
|
||||
import com.quinn.utils.SpringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.mail.Authenticator;
|
||||
import javax.mail.Message;
|
||||
import javax.mail.PasswordAuthentication;
|
||||
@@ -9,15 +18,16 @@ import javax.mail.Session;
|
||||
import javax.mail.Transport;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* 使用@multiavatar生成头像
|
||||
* https://api.multiavatar.com/
|
||||
*/
|
||||
public enum SendBMail {
|
||||
@Component
|
||||
public class SendBMail {
|
||||
|
||||
INSTANCE;
|
||||
private static final Logger log = LoggerFactory.getLogger(SendBMail.class);
|
||||
final String username = "quinn.admin@88.com";
|
||||
final String password = "Y3YYd4hcY2fFB9VY";
|
||||
@@ -27,14 +37,18 @@ public enum SendBMail {
|
||||
boolean isAuth = true;
|
||||
String from = "quinn.admin@88.com";
|
||||
|
||||
public boolean sendRegisterMail(String registerCode,String email){
|
||||
return sendMail("QUINN注册码",
|
||||
"您好,您的注册码是【" + registerCode + "】",email);
|
||||
@Resource
|
||||
EmailService emailService;
|
||||
|
||||
public boolean sendOneParamMail(EmailType emailType, String param, String email){
|
||||
// 可以设计枚举,使用同一个发送邮件的公共方法
|
||||
Email register = emailService.getOne(new QueryWrapper<Email>().eq("type",emailType.name()));
|
||||
String content = register.getContent().replace(QuinnConstant.EMAIL_REPLACE, param);
|
||||
return sendMail(register.getTitle(), content,email);
|
||||
}
|
||||
|
||||
public boolean sendWelcome(String username,String email){
|
||||
return sendMail("QUINN-注册成功",
|
||||
username + "您好,欢迎使用!",email);
|
||||
public boolean sendParamsMail(EmailType emailType, String email, Map<String,String> map){
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean sendMail(String subject,String content,String toEmail){
|
||||
@@ -60,6 +74,7 @@ public enum SendBMail {
|
||||
log.error("发送邮件失败",e);
|
||||
return false;
|
||||
}
|
||||
log.info("发送邮件成功"+toEmail);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
16
src/main/java/com/quinn/mapper/EmailMapper.java
Normal file
16
src/main/java/com/quinn/mapper/EmailMapper.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.quinn.mapper;
|
||||
|
||||
import com.quinn.pojo.Email;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Mapper 接口
|
||||
* </p>
|
||||
*
|
||||
* @author limqsh
|
||||
* @since 2022-05-15
|
||||
*/
|
||||
public interface EmailMapper extends BaseMapper<Email> {
|
||||
|
||||
}
|
||||
5
src/main/java/com/quinn/mapper/xml/EmailMapper.xml
Normal file
5
src/main/java/com/quinn/mapper/xml/EmailMapper.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.quinn.mapper.EmailMapper">
|
||||
|
||||
</mapper>
|
||||
48
src/main/java/com/quinn/pojo/Email.java
Normal file
48
src/main/java/com/quinn/pojo/Email.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package com.quinn.pojo;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import java.util.Date;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import java.io.Serializable;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
*
|
||||
* </p>
|
||||
*
|
||||
* @author limqsh
|
||||
* @since 2022-05-15
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
@TableName("qn_email")
|
||||
@ApiModel(value="Email对象", description="")
|
||||
public class Email implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@ApiModelProperty(value = "唯一id")
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
@ApiModelProperty(value = "类型")
|
||||
private String type;
|
||||
|
||||
@ApiModelProperty(value = "标题")
|
||||
private String title;
|
||||
|
||||
@ApiModelProperty(value = "内容")
|
||||
private String content;
|
||||
|
||||
@ApiModelProperty(value = "时间")
|
||||
private Date gmtCreate;
|
||||
|
||||
|
||||
}
|
||||
16
src/main/java/com/quinn/service/EmailService.java
Normal file
16
src/main/java/com/quinn/service/EmailService.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.quinn.service;
|
||||
|
||||
import com.quinn.pojo.Email;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 服务类
|
||||
* </p>
|
||||
*
|
||||
* @author limqsh
|
||||
* @since 2022-05-15
|
||||
*/
|
||||
public interface EmailService extends IService<Email> {
|
||||
|
||||
}
|
||||
20
src/main/java/com/quinn/service/impl/EmailServiceImpl.java
Normal file
20
src/main/java/com/quinn/service/impl/EmailServiceImpl.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.quinn.service.impl;
|
||||
|
||||
import com.quinn.pojo.Email;
|
||||
import com.quinn.mapper.EmailMapper;
|
||||
import com.quinn.service.EmailService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @author limqsh
|
||||
* @since 2022-05-15
|
||||
*/
|
||||
@Service
|
||||
public class EmailServiceImpl extends ServiceImpl<EmailMapper, Email> implements EmailService {
|
||||
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.quinn.task;
|
||||
|
||||
import com.quinn.common.EmailType;
|
||||
import com.quinn.intergration.SendBMail;
|
||||
import com.quinn.utils.SpringUtils;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@@ -17,6 +19,7 @@ public class SendAsyncEmail implements Runnable{
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
SendBMail.INSTANCE.sendWelcome(username,email);
|
||||
SendBMail sendBMail = SpringUtils.getBean(SendBMail.class);
|
||||
sendBMail.sendOneParamMail(EmailType.SUCCESS,username,email);
|
||||
}
|
||||
}
|
||||
|
||||
34
src/main/java/com/quinn/utils/SpringUtils.java
Normal file
34
src/main/java/com/quinn/utils/SpringUtils.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package com.quinn.utils;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SpringUtils implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
/**
|
||||
* 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
|
||||
*/
|
||||
public static<T> T getBean(Class<T> tClass) {
|
||||
return getApplicationContext().getBean(tClass);
|
||||
}
|
||||
|
||||
public static<T> T getBean(String name,Class<T> tClass) {
|
||||
return getApplicationContext().getBean(name,tClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
if (SpringUtils.applicationContext == null){
|
||||
SpringUtils.applicationContext = applicationContext;
|
||||
}
|
||||
}
|
||||
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return applicationContext;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user