🔨 移动common下部分模块至starter下
This commit is contained in:
21
ballcat-starters/ballcat-spring-boot-starter-mail/pom.xml
Normal file
21
ballcat-starters/ballcat-spring-boot-starter-mail/pom.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>ballcat-starters</artifactId>
|
||||
<groupId>com.hccake</groupId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>ballcat-spring-boot-starter-mail</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.hccake.ballcat.common.mail.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.File;
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* @version 1.0
|
||||
* @date 2020/2/27 17:06
|
||||
*/
|
||||
@Data
|
||||
public class MailDTO {
|
||||
/**
|
||||
* 发件人
|
||||
*/
|
||||
private String from;
|
||||
/**
|
||||
* 收件人(多个邮箱则用逗号","隔开)
|
||||
*/
|
||||
private String to;
|
||||
/**
|
||||
* 邮件主题
|
||||
*/
|
||||
private String subject;
|
||||
/**
|
||||
* 是否渲染html
|
||||
*/
|
||||
private Boolean showHtml;
|
||||
/**
|
||||
* 邮件内容
|
||||
*/
|
||||
private String content;
|
||||
/**
|
||||
* 抄送(多个邮箱则用逗号","隔开)
|
||||
*/
|
||||
private String cc;
|
||||
/**
|
||||
* 密送(多个邮箱则用逗号","隔开)
|
||||
*/
|
||||
private String bcc;
|
||||
/**
|
||||
* 附件
|
||||
*/
|
||||
private File[] files;
|
||||
/**
|
||||
* 发送时间
|
||||
*/
|
||||
private LocalDate sentDate;
|
||||
/**
|
||||
* 发送状态 1:成功 2:失败
|
||||
*/
|
||||
private Integer status;
|
||||
/**
|
||||
* 错误信息 errorMsg
|
||||
*/
|
||||
private String errorMsg;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.hccake.ballcat.common.mail.event;
|
||||
|
||||
import com.hccake.ballcat.common.mail.dto.MailDTO;
|
||||
import lombok.ToString;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* @version 1.0
|
||||
* @date 2020/2/27 18:00
|
||||
*/
|
||||
@ToString
|
||||
public class MailSendEvent extends ApplicationEvent {
|
||||
|
||||
public MailSendEvent(MailDTO mailDTO) {
|
||||
super(mailDTO);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.hccake.ballcat.common.mail.listener;
|
||||
|
||||
import com.hccake.ballcat.common.mail.event.MailSendEvent;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
/**
|
||||
* 邮件发送事件监听器
|
||||
* 可在这处理邮件记录
|
||||
* @author Hccake
|
||||
* @version 1.0
|
||||
* @date 2020/2/27 18:11
|
||||
*/
|
||||
@Slf4j
|
||||
public class MailSentListener implements ApplicationListener<MailSendEvent> {
|
||||
@Override
|
||||
public void onApplicationEvent(MailSendEvent mailSendEvent) {
|
||||
log.info(mailSendEvent.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.hccake.ballcat.common.mail.service;
|
||||
|
||||
import com.hccake.ballcat.common.mail.dto.MailDTO;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* @version 1.0
|
||||
* @date 2020/2/27 17:05
|
||||
*/
|
||||
public interface MailSender {
|
||||
|
||||
/**
|
||||
* 发送邮件
|
||||
* @param mailDTO 邮件信息
|
||||
*/
|
||||
void sendMail(MailDTO mailDTO);
|
||||
|
||||
|
||||
/**
|
||||
* 发送邮件
|
||||
* @param to 收件人,多个邮箱使用,号间隔
|
||||
* @param subject 主题
|
||||
* @param content 邮件正文
|
||||
* @param showHtml 是否将正文渲染为html
|
||||
*/
|
||||
default void sendMail(String to, String subject, String content, boolean showHtml){
|
||||
MailDTO mailDTO = new MailDTO();
|
||||
mailDTO.setShowHtml(showHtml);
|
||||
mailDTO.setSubject(subject);
|
||||
mailDTO.setContent(content);
|
||||
mailDTO.setTo(to);
|
||||
sendMail(mailDTO);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送普通文本邮件
|
||||
* @param to 收件人,多个邮箱使用,号间隔
|
||||
* @param subject 主题
|
||||
* @param content 邮件正文
|
||||
*/
|
||||
default void sendTextMail(String to, String subject, String content){
|
||||
sendMail(to, subject, content, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送Html邮件
|
||||
* @param to 收件人,多个邮箱使用,号间隔
|
||||
* @param subject 主题
|
||||
* @param content 邮件正文
|
||||
*/
|
||||
default void sendHtmlMail(String to, String subject, String content){
|
||||
sendMail(to, subject, content, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查邮件是否符合标准
|
||||
* @param mailDTO 邮件信息
|
||||
*/
|
||||
default void checkMail(MailDTO mailDTO) {
|
||||
if (StringUtils.isEmpty(mailDTO.getTo())) {
|
||||
throw new RuntimeException("邮件收信人不能为空");
|
||||
}
|
||||
if (StringUtils.isEmpty(mailDTO.getSubject())) {
|
||||
throw new RuntimeException("邮件主题不能为空");
|
||||
}
|
||||
if (StringUtils.isEmpty(mailDTO.getContent())) {
|
||||
throw new RuntimeException("邮件内容不能为空");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.hccake.ballcat.common.mail.service.impl;
|
||||
|
||||
import com.hccake.ballcat.common.mail.dto.MailDTO;
|
||||
import com.hccake.ballcat.common.mail.event.MailSendEvent;
|
||||
import com.hccake.ballcat.common.mail.service.MailSender;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* @version 1.0
|
||||
* @date 2020/2/27 17:06
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class MailSenderImpl implements MailSender {
|
||||
private final JavaMailSender mailSender;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
/**
|
||||
* 配置文件中我的qq邮箱
|
||||
*/
|
||||
@Value("${spring.mail.properties.from}")
|
||||
private String defaultFrom;
|
||||
|
||||
|
||||
/**
|
||||
* 发送邮件
|
||||
*
|
||||
* @param mailDTO
|
||||
*/
|
||||
@Override
|
||||
public void sendMail(MailDTO mailDTO) {
|
||||
try {
|
||||
//1.检测邮件
|
||||
checkMail(mailDTO);
|
||||
//2.发送邮件
|
||||
sendMimeMail(mailDTO);
|
||||
mailDTO.setStatus(1);
|
||||
} catch (Exception e) {
|
||||
mailDTO.setStatus(2);
|
||||
mailDTO.setErrorMsg(e.getMessage());
|
||||
log.error("发送邮件失败:", e);
|
||||
}finally {
|
||||
// 发布邮件发送事件
|
||||
applicationContext.publishEvent(new MailSendEvent(mailDTO));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构建复杂邮件信息类
|
||||
* @param mailDTO
|
||||
*/
|
||||
private void sendMimeMail(MailDTO mailDTO) {
|
||||
try {
|
||||
// true表示支持复杂类型
|
||||
MimeMessageHelper messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true);
|
||||
String from = StringUtils.isEmpty(mailDTO.getFrom())? defaultFrom: mailDTO.getFrom();
|
||||
messageHelper.setFrom(from);
|
||||
messageHelper.setTo(mailDTO.getTo().split(","));
|
||||
messageHelper.setSubject(mailDTO.getSubject());
|
||||
// 是否展示html
|
||||
boolean showHtml = mailDTO.getShowHtml() == null ? false: mailDTO.getShowHtml();
|
||||
messageHelper.setText(mailDTO.getContent(), showHtml);
|
||||
if (!StringUtils.isEmpty(mailDTO.getCc())) {
|
||||
messageHelper.setCc(mailDTO.getCc().split(","));
|
||||
}
|
||||
if (!StringUtils.isEmpty(mailDTO.getBcc())) {
|
||||
messageHelper.setCc(mailDTO.getBcc().split(","));
|
||||
}
|
||||
if (mailDTO.getFiles() != null) {
|
||||
for (File file : mailDTO.getFiles()) {
|
||||
messageHelper.addAttachment(file.getName(), file);
|
||||
}
|
||||
}
|
||||
mailSender.send(messageHelper.getMimeMessage());
|
||||
log.info("发送邮件成功:{}->{}", from, mailDTO.getTo());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.hccake.ballcat.common.mail.service.impl.MailSenderImpl,\
|
||||
com.hccake.ballcat.common.mail.listener.MailSentListener
|
||||
Reference in New Issue
Block a user