parent
1cafe91eda
commit
ed7a420bce
@ -0,0 +1,20 @@
|
||||
package org.opsli.plugins.mail;
|
||||
|
||||
import org.opsli.plugins.mail.model.MailModel;
|
||||
|
||||
/**
|
||||
* @BelongsProject: opsli-boot
|
||||
* @BelongsPackage: org.opsli.plugins.mail.handler
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-13 18:51
|
||||
* @Description: 邮件执行器
|
||||
*/
|
||||
public interface MailHandler {
|
||||
|
||||
/**
|
||||
* 发送邮件
|
||||
* @param mailModel
|
||||
*/
|
||||
void send(MailModel mailModel);
|
||||
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package org.opsli.plugins.mail;
|
||||
|
||||
/**
|
||||
* @BelongsProject: opsli-boot
|
||||
* @BelongsPackage: org.opsli.plugins.mail
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-12 22:51
|
||||
* @Description: 邮件工具
|
||||
*/
|
||||
public enum MailUtil {
|
||||
|
||||
/** 实例*/
|
||||
INSTANCE;
|
||||
|
||||
/**
|
||||
* 发送邮件
|
||||
*/
|
||||
public void send(){
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package org.opsli.plugins.mail.exception;
|
||||
|
||||
import org.opsli.common.base.msg.BaseMsg;
|
||||
import org.opsli.common.exception.ServiceException;
|
||||
|
||||
/**
|
||||
* @BelongsProject: opsli-boot
|
||||
* @BelongsPackage: org.opsli.plugins.mail.exception
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-13 18:44
|
||||
* @Description: 邮件异常
|
||||
*/
|
||||
public class MailException extends ServiceException {
|
||||
|
||||
public MailException(Integer code, String errorMessage) {
|
||||
super(code, errorMessage);
|
||||
}
|
||||
|
||||
public MailException(BaseMsg msg) {
|
||||
super(msg);
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package org.opsli.plugins.mail.handler;
|
||||
|
||||
import cn.hutool.core.lang.Validator;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.opsli.common.exception.EmptyException;
|
||||
import org.opsli.plugins.mail.MailHandler;
|
||||
import org.opsli.plugins.mail.exception.MailException;
|
||||
import org.opsli.plugins.mail.model.MailModel;
|
||||
import org.opsli.plugins.mail.msg.MailMsg;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.mail.internet.MimeMessage;
|
||||
|
||||
/**
|
||||
* @BelongsProject: opsli-boot
|
||||
* @BelongsPackage: org.opsli.plugins.mail.handler
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-13 18:52
|
||||
* @Description: 邮件执行器 实现类
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class MailHandlerImpl implements MailHandler {
|
||||
|
||||
@Value("${spring.mail.username}")
|
||||
private String username;
|
||||
|
||||
@Autowired
|
||||
private JavaMailSender javaMailSender;
|
||||
|
||||
@Override
|
||||
public void send(MailModel mailModel) {
|
||||
try {
|
||||
MimeMessage message= javaMailSender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(message,true);
|
||||
helper.setFrom(username);
|
||||
helper.setTo(mailModel.getTo());
|
||||
helper.setSubject(mailModel.getSubject());
|
||||
helper.setText(mailModel.getContent(),true);
|
||||
javaMailSender.send(message);
|
||||
} catch (Exception e) {
|
||||
log.error("邮件发送异常:{}",e.getMessage());
|
||||
throw new MailException(MailMsg.EXCEPTION_UNKNOWN);
|
||||
}
|
||||
log.info("邮件发送 - 发送至:{} - 主题:{}", mailModel.getTo(),mailModel.getSubject());
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验发送邮件的请求参数
|
||||
*/
|
||||
private void validationMailModel(MailModel mailModel) {
|
||||
if(Validator.isEmpty(mailModel) ||
|
||||
Validator.isEmpty(mailModel.getTo()) ||
|
||||
Validator.isEmpty(mailModel.getSubject()) ||
|
||||
Validator.isEmpty(mailModel.getContent())){
|
||||
throw new EmptyException();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package org.opsli.plugins.mail.model;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* @BelongsProject: opsli-boot
|
||||
* @BelongsPackage: org.opsli.plugins.mail.model
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-13 18:46
|
||||
* @Description: 邮件传输类
|
||||
*/
|
||||
@Data
|
||||
@ToString
|
||||
public class MailModel {
|
||||
|
||||
/** 收件人 */
|
||||
private String to;
|
||||
|
||||
/** 邮件主题 */
|
||||
private String subject;
|
||||
|
||||
/** 邮件内容 */
|
||||
private String content;
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package org.opsli.plugins.mail.msg;
|
||||
|
||||
import org.opsli.common.base.msg.BaseMsg;
|
||||
|
||||
/**
|
||||
* @BelongsProject: opsli-boot
|
||||
* @BelongsPackage: org.opsli.plugins.mail.msg
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-13 19:54
|
||||
* @Description: 邮件消息
|
||||
*/
|
||||
public enum MailMsg implements BaseMsg {
|
||||
|
||||
/** 未知消息异常 */
|
||||
EXCEPTION_UNKNOWN(90001,"邮件发送失败"),
|
||||
;
|
||||
|
||||
|
||||
private int code;
|
||||
private String message;
|
||||
|
||||
MailMsg(int code,String message){
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
<?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>
|
||||
<groupId>org.opsliframework.boot</groupId>
|
||||
<artifactId>opsli-boot-parent</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>opsli-plugins</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<!-- 插件 -->
|
||||
<modules>
|
||||
<module>opsli-plugins-mail</module>
|
||||
</modules>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入公共模块 -->
|
||||
<dependency>
|
||||
<groupId>org.opsliframework.boot</groupId>
|
||||
<artifactId>opsli-common</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
Loading…
Reference in new issue