邮件插件

v1.4.1
Parker 4 years ago
parent 1cafe91eda
commit ed7a420bce

@ -1,3 +1,5 @@
**建议在 当前 plugins 父类pom中定义子maven所引用的版本号**
## opsli 插件类
1. mail 邮件插件

@ -3,14 +3,22 @@
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>opsli-boot-parent</artifactId>
<artifactId>opsli-plugins</artifactId>
<groupId>org.opsliframework.boot</groupId>
<version>1.0.0</version>
<relativePath>../../pom.xml</relativePath>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>opsli-plugins-mail</artifactId>
<version>${project.parent.version}</version>
<dependencies>
<!-- 邮件服务 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
</project>

@ -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…
Cancel
Save