fix:完善Email Monitor
This commit is contained in:
parent
997f1af022
commit
dbff5a719b
|
@ -159,6 +159,65 @@ public class EmailServiceManager {
|
|||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* 测试收件邮箱账号是否可以正常使用
|
||||
* */
|
||||
public boolean canReceive(){
|
||||
Integer port = email.getPort();
|
||||
String username = email.getUsername();
|
||||
String password = email.getPassword();
|
||||
String host = email.getEmailServerAddress();
|
||||
|
||||
Properties props = new Properties();
|
||||
props.put("mail.store.protocol","imap");
|
||||
props.put("mail.imap.host", host);
|
||||
props.put("mail.imap.port", port);
|
||||
|
||||
Session session = Session.getInstance(props, new Authenticator() {
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(username, password);
|
||||
}
|
||||
});
|
||||
|
||||
try(Store store = session.getStore()) {
|
||||
store.connect(host, username, password);
|
||||
return store.isConnected();
|
||||
} catch (Exception e) {
|
||||
log.error("收件邮箱服务[Host:{}, Port:{}, Username:{}]连接异常: {}",host, port, username, e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 测试发件邮箱账号是否可以正常使用
|
||||
* */
|
||||
public boolean canSend(){
|
||||
Integer port = email.getPort();
|
||||
String username = email.getUsername();
|
||||
String password = email.getPassword();
|
||||
String host = email.getEmailServerAddress();
|
||||
|
||||
Properties props = new Properties();
|
||||
props.put("mail.transport.protocol", "smtp");
|
||||
props.put("mail.smtp.host", host);
|
||||
props.put("mail.smtp.port", port);
|
||||
props.put("mail.smtp.auth", "true");
|
||||
|
||||
Session session = Session.getInstance(props, new Authenticator() {
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(username, password);
|
||||
}
|
||||
});
|
||||
|
||||
try (Transport transport = session.getTransport()){
|
||||
transport.connect(host, username, password);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("发件邮箱服务[Host:{}, Port:{}, Username:{}]连接异常: {}",host, port, username, e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送邮件 单发
|
||||
*/
|
||||
|
|
|
@ -1,8 +1,14 @@
|
|||
package org.jeecg.common.email.emuns;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 邮件类型
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum SysMailType {
|
||||
|
||||
/**
|
||||
|
@ -14,13 +20,15 @@ public enum SysMailType {
|
|||
*/
|
||||
SEND_EMAIL(2);
|
||||
|
||||
private Integer emailType;
|
||||
private final Integer emailType;
|
||||
|
||||
SysMailType(int emailType) {
|
||||
this.emailType = emailType;
|
||||
public static SysMailType valueOf(Integer value){
|
||||
if (ObjectUtil.isNull(value))
|
||||
return null;
|
||||
for (SysMailType sysMailType : SysMailType.values()) {
|
||||
if (sysMailType.getEmailType().compareTo(value) == 0)
|
||||
return sysMailType;
|
||||
}
|
||||
|
||||
public Integer getEmailType(){
|
||||
return this.emailType;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +1,30 @@
|
|||
package org.jeecg.common.util;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.common.email.EmailServiceManager;
|
||||
import org.jeecg.common.email.emuns.SysMailType;
|
||||
import org.jeecg.modules.base.entity.postgre.SysEmail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
@Slf4j
|
||||
public class EmailUtil {
|
||||
|
||||
public static boolean isConnection(String address, Integer port){
|
||||
try(Socket socket = new Socket()){
|
||||
InetSocketAddress socketAddress = new InetSocketAddress(address, port);
|
||||
socket.connect(socketAddress,5000);
|
||||
return true;
|
||||
}catch (IOException e){
|
||||
log.error("EmailUtil.isConnection():邮箱服务[{}]连接失败: {}", address, e.getMessage());
|
||||
public static boolean isConnection(SysEmail email){
|
||||
EmailServiceManager manager = EmailServiceManager.getInstance();
|
||||
manager.init(email);
|
||||
Integer emilType = email.getEmilType();
|
||||
SysMailType emailType = SysMailType.valueOf(emilType);
|
||||
if (ObjectUtil.isNotNull(emailType)){
|
||||
switch (emailType){
|
||||
case RECEIVE_EMAIL:
|
||||
return manager.canReceive();
|
||||
case SEND_EMAIL:
|
||||
return manager.canSend();
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ public class DatabaseStatusManager {
|
|||
}
|
||||
|
||||
private void init(){
|
||||
sleepTime = 30 * 60 * 1000; // 睡眠时间30min
|
||||
sleepTime = 5 * 1000; // 睡眠时间30min
|
||||
redisUtil = SpringContextUtils.getBean(RedisUtil.class);
|
||||
databaseService = SpringContextUtils.getBean(ISysDatabaseService.class);
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ public class EmailStatusManager {
|
|||
}
|
||||
|
||||
private void init(){
|
||||
sleepTime = 30 * 60 * 1000; // 睡眠时间30min
|
||||
sleepTime = 5 * 1000; // 睡眠时间30min
|
||||
emailService = SpringContextUtils.getBean(ISysEmailService.class);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ import org.jeecg.modules.base.dto.SourceDto;
|
|||
import org.jeecg.modules.base.entity.postgre.SysDatabase;
|
||||
import org.jeecg.modules.base.entity.postgre.SysEmail;
|
||||
import org.jeecg.modules.base.bizVo.SourceVo;
|
||||
import org.jeecg.modules.base.enums.Enabled;
|
||||
import org.jeecg.modules.entity.AlarmHistory;
|
||||
import org.jeecg.modules.mapper.SysEmailMapper;
|
||||
import org.jeecg.modules.service.ISysEmailService;
|
||||
|
@ -35,6 +36,8 @@ import java.time.format.DateTimeFormatter;
|
|||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.jeecg.modules.base.enums.Enabled.ENABLED;
|
||||
|
||||
@Service("sysEmailService")
|
||||
public class SysEmailServiceImpl extends ServiceImpl<SysEmailMapper, SysEmail> implements ISysEmailService {
|
||||
|
||||
|
@ -186,7 +189,7 @@ public class SysEmailServiceImpl extends ServiceImpl<SysEmailMapper, SysEmail> i
|
|||
public Result<SysEmail> getSender() {
|
||||
LambdaQueryWrapper<SysEmail> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(SysEmail::getEmilType, SysMailType.SEND_EMAIL.getEmailType());
|
||||
wrapper.eq(SysEmail::getEnabled, CommonConstant.ENABLED);
|
||||
wrapper.eq(SysEmail::getEnabled, ENABLED.getValue());
|
||||
List<SysEmail> emails = this.list(wrapper);
|
||||
SysEmail sysEmail = emails.stream().findFirst().get();
|
||||
return Result.OK(sysEmail);
|
||||
|
@ -214,9 +217,7 @@ public class SysEmailServiceImpl extends ServiceImpl<SysEmailMapper, SysEmail> i
|
|||
Map<String, Object> statusMap = new HashMap<>();
|
||||
for (SysEmail email : emails) {
|
||||
String id = email.getId();
|
||||
String address = email.getEmailServerAddress();
|
||||
Integer port = email.getPort();
|
||||
boolean isConn = EmailUtil.isConnection(address, port);
|
||||
boolean isConn = EmailUtil.isConnection(email);
|
||||
statusMap.put(id, isConn);
|
||||
}
|
||||
// 将邮箱服务器连接状态更新到reids
|
||||
|
@ -231,9 +232,7 @@ public class SysEmailServiceImpl extends ServiceImpl<SysEmailMapper, SysEmail> i
|
|||
* */
|
||||
private void saveOrUpdateStatus(SysEmail email){
|
||||
String id = email.getId();
|
||||
String address = email.getEmailServerAddress();
|
||||
Integer port = email.getPort();
|
||||
boolean isConn = EmailUtil.isConnection(address, port);
|
||||
boolean isConn = EmailUtil.isConnection(email);
|
||||
String prefixStatus = RedisConstant.PREFIX_STATUS;
|
||||
String emailStatus = RedisConstant.EMAIL_STATUS;
|
||||
String statusKey = prefixStatus + emailStatus;
|
||||
|
|
Loading…
Reference in New Issue
Block a user