feat:发送单封邮件
This commit is contained in:
parent
89c5377c09
commit
eebe1d8aeb
|
@ -24,6 +24,11 @@ public class MessageDTO implements Serializable {
|
||||||
*/
|
*/
|
||||||
protected String toUser;
|
protected String toUser;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 用户名-Email
|
||||||
|
* */
|
||||||
|
protected Map<String, String> userEmail;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送给所有人
|
* 发送给所有人
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package org.jeecg.common.email;
|
package org.jeecg.common.email;
|
||||||
|
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.sun.mail.imap.IMAPStore;
|
import com.sun.mail.imap.IMAPStore;
|
||||||
|
@ -14,6 +15,7 @@ import org.jetbrains.annotations.NotNull;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.mail.*;
|
import javax.mail.*;
|
||||||
|
import javax.mail.internet.AddressException;
|
||||||
import javax.mail.internet.InternetAddress;
|
import javax.mail.internet.InternetAddress;
|
||||||
import javax.mail.internet.MimeMessage;
|
import javax.mail.internet.MimeMessage;
|
||||||
import javax.mail.internet.MimeUtility;
|
import javax.mail.internet.MimeUtility;
|
||||||
|
@ -152,7 +154,44 @@ public class EmailServiceManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送邮件
|
* 发送邮件 单发
|
||||||
|
*/
|
||||||
|
public void sendMailToOne(MessageDTO messageDTO) throws MessagingException {
|
||||||
|
// 邮箱连接属性
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.put("mail.transport.protocol", "smtp");
|
||||||
|
props.put("mail.smtp.host", email.getEmailServerAddress());
|
||||||
|
props.put("mail.smtp.port", email.getPort());
|
||||||
|
props.put("mail.smtp.auth", "true");
|
||||||
|
/*props.put("mail.smtp.socketFactory.port", email.getPort());
|
||||||
|
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");*/
|
||||||
|
|
||||||
|
Session session = Session.getInstance(props);
|
||||||
|
|
||||||
|
String toUser = messageDTO.getToUser();
|
||||||
|
|
||||||
|
Message message = new MimeMessage(session);
|
||||||
|
// 邮件发送方(邮箱必须真实有效)
|
||||||
|
message.setFrom(new InternetAddress(email.getUsername()));
|
||||||
|
// 设置主题
|
||||||
|
message.setSubject(messageDTO.getTitle());
|
||||||
|
// 设置正文
|
||||||
|
message.setText(messageDTO.getContent());
|
||||||
|
// 设置收件地址
|
||||||
|
message.setRecipient(Message.RecipientType.TO,
|
||||||
|
new InternetAddress(toUser));
|
||||||
|
|
||||||
|
// 发送邮件
|
||||||
|
Transport transport = session.getTransport();
|
||||||
|
transport.connect(email.getUsername(),email.getPassword());
|
||||||
|
transport.sendMessage(message, message.getAllRecipients());
|
||||||
|
|
||||||
|
// 关闭资源
|
||||||
|
transport.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送邮件 群发
|
||||||
*/
|
*/
|
||||||
public void sendMail(MessageDTO messageDTO){
|
public void sendMail(MessageDTO messageDTO){
|
||||||
// 邮箱连接属性
|
// 邮箱连接属性
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
package org.jeecg.modules;
|
||||||
|
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.sun.mail.imap.IMAPFolder;
|
||||||
|
import com.sun.mail.imap.IMAPStore;
|
||||||
|
|
||||||
|
import javax.mail.*;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class Demo {
|
||||||
|
|
||||||
|
/*public static void main(String[] args) {
|
||||||
|
String host = "imap.163.com";
|
||||||
|
String username = "nzyone@163.com";
|
||||||
|
String password = "NHFYZPXSPBKZUXTJ";
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 创建连接属性
|
||||||
|
Properties props = new Properties();
|
||||||
|
props.setProperty("mail.store.protocol", "imap");
|
||||||
|
props.put("mail.imap.port", "143");
|
||||||
|
// props.put("mail.imap.ssl.enable", "true");
|
||||||
|
|
||||||
|
Map<String, String> IAM = new HashMap();
|
||||||
|
//带上IMAP ID信息,由key和value组成,例如name,version,vendor,support-email等。
|
||||||
|
IAM.put("name","liuyang");
|
||||||
|
IAM.put("version","1.0.0");
|
||||||
|
IAM.put("vendor","myclient");
|
||||||
|
IAM.put("support-email","nzyone@163.com");
|
||||||
|
Session session = Session.getInstance(props);
|
||||||
|
IMAPStore store = (IMAPStore) session.getStore("imap");
|
||||||
|
|
||||||
|
// 创建会话
|
||||||
|
//Session session = Session.getInstance(props);
|
||||||
|
|
||||||
|
// 创建IMAP存储对象并连接到服务器
|
||||||
|
//Store store = session.getStore("imap");
|
||||||
|
store.connect(host, username, password);
|
||||||
|
|
||||||
|
store.id(IAM);
|
||||||
|
|
||||||
|
Quota[] quotas = store.getQuota("INBOX");
|
||||||
|
|
||||||
|
// 打开收件箱
|
||||||
|
//IMAPFolder inbox = (IMAPFolder) store.getFolder("INBOX");
|
||||||
|
//inbox.open(Folder.READ_ONLY);
|
||||||
|
|
||||||
|
// 获取邮箱容量信息
|
||||||
|
//Quota[] quotas = inbox.getQuota();
|
||||||
|
|
||||||
|
for (Quota quota : quotas) {
|
||||||
|
Quota.Resource[] resources = quota.resources;
|
||||||
|
|
||||||
|
for (Quota.Resource res : resources) {
|
||||||
|
long usage = res.usage;
|
||||||
|
long limit = res.limit;
|
||||||
|
|
||||||
|
System.out.println("Resource: " + res);
|
||||||
|
System.out.println("Usage: " + usage);
|
||||||
|
System.out.println("Limit: " + limit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 关闭连接
|
||||||
|
// inbox.close(false);
|
||||||
|
store.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
double a = 4.148059e+03;
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,13 +2,22 @@ package org.jeecg.modules.controller;
|
||||||
|
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import org.jeecg.common.api.dto.message.MessageDTO;
|
||||||
import org.jeecg.common.api.vo.Result;
|
import org.jeecg.common.api.vo.Result;
|
||||||
|
import org.jeecg.common.email.EmailServiceManager;
|
||||||
|
import org.jeecg.common.util.SpringContextUtils;
|
||||||
import org.jeecg.modules.base.entity.postgre.AlarmLog;
|
import org.jeecg.modules.base.entity.postgre.AlarmLog;
|
||||||
|
import org.jeecg.modules.base.entity.postgre.SysEmail;
|
||||||
import org.jeecg.modules.service.IAlarmLogService;
|
import org.jeecg.modules.service.IAlarmLogService;
|
||||||
import org.jeecg.modules.base.bizVo.AlarmVo;
|
import org.jeecg.modules.base.bizVo.AlarmVo;
|
||||||
|
import org.jeecg.modules.service.ISysEmailService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.mail.MessagingException;
|
||||||
|
|
||||||
|
import static org.jeecg.common.email.EmailServiceManager.getInstance;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("alarmLog")
|
@RequestMapping("alarmLog")
|
||||||
@Api(value = "报警日志服务", tags = "报警日志服务")
|
@Api(value = "报警日志服务", tags = "报警日志服务")
|
||||||
|
|
|
@ -10,5 +10,5 @@ public interface CalculateConcService {
|
||||||
|
|
||||||
boolean calcAndSave();
|
boolean calcAndSave();
|
||||||
|
|
||||||
Map<String,String> calculate(List<ConcDto> concDtos, BigDecimal index);
|
Map<String, String> calculate(List<ConcDto> concDtos, BigDecimal index);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package org.jeecg.modules.message;
|
package org.jeecg.modules.message;
|
||||||
|
|
||||||
import cn.hutool.core.collection.ListUtil;
|
import cn.hutool.core.collection.ListUtil;
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import org.jeecg.common.api.dto.message.MessageDTO;
|
import org.jeecg.common.api.dto.message.MessageDTO;
|
||||||
import org.jeecg.common.config.mqtoken.UserTokenContext;
|
import org.jeecg.common.config.mqtoken.UserTokenContext;
|
||||||
|
@ -69,8 +70,11 @@ public class SendMessage {
|
||||||
} else if (way.equals(YJ.getValue())) {// 2.推送邮箱
|
} else if (way.equals(YJ.getValue())) {// 2.推送邮箱
|
||||||
String toEmail = contact.get(Email);
|
String toEmail = contact.get(Email);
|
||||||
if (StrUtil.isNotBlank(toEmail)){
|
if (StrUtil.isNotBlank(toEmail)){
|
||||||
messageDTO.setToUser(toEmail);
|
// messageDTO.setToUser(toEmail);
|
||||||
messageDTO.setType(YJ.getType());
|
messageDTO.setType(YJ.getType());
|
||||||
|
Map<String, String> userEmail = new HashMap<>(contact);
|
||||||
|
MapUtil.removeAny(userEmail, Sms, Email, System);
|
||||||
|
messageDTO.setUserEmail(userEmail);
|
||||||
sysBaseAPI.sendTemplateMessage(messageDTO);
|
sysBaseAPI.sendTemplateMessage(messageDTO);
|
||||||
}
|
}
|
||||||
} else if (way.equals(SMS.getValue())) {// 3.推送短信
|
} else if (way.equals(SMS.getValue())) {// 3.推送短信
|
||||||
|
@ -103,6 +107,9 @@ public class SendMessage {
|
||||||
.filter(user -> StrUtil.isNotBlank(user.getEmail()))
|
.filter(user -> StrUtil.isNotBlank(user.getEmail()))
|
||||||
.map(SysUser::getEmail)
|
.map(SysUser::getEmail)
|
||||||
.collect(Collectors.joining(comma));
|
.collect(Collectors.joining(comma));
|
||||||
|
// 用户名-邮箱Map
|
||||||
|
Map<String, String> userEmail = sysUsers.stream()
|
||||||
|
.collect(Collectors.toMap(SysUser::getUsername, SysUser::getEmail));
|
||||||
// 手机号码
|
// 手机号码
|
||||||
String phoneList = sysUsers.stream()
|
String phoneList = sysUsers.stream()
|
||||||
.filter(user -> StrUtil.isNotBlank(user.getPhone()))
|
.filter(user -> StrUtil.isNotBlank(user.getPhone()))
|
||||||
|
@ -112,6 +119,7 @@ public class SendMessage {
|
||||||
result.put(System,usernameList);
|
result.put(System,usernameList);
|
||||||
result.put(Email,emailList);
|
result.put(Email,emailList);
|
||||||
result.put(Sms,phoneList);
|
result.put(Sms,phoneList);
|
||||||
|
result.putAll(userEmail);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package org.jeecg.modules.message.handle.impl;
|
package org.jeecg.modules.message.handle.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.sun.mail.smtp.SMTPAddressFailedException;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.jeecg.common.api.dto.message.MessageDTO;
|
import org.jeecg.common.api.dto.message.MessageDTO;
|
||||||
import org.jeecg.common.email.EmailServiceManager;
|
import org.jeecg.common.email.EmailServiceManager;
|
||||||
|
@ -10,6 +12,11 @@ import org.jeecg.modules.message.service.ISysMessageService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.mail.MessagingException;
|
||||||
|
import javax.mail.SendFailedException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.jeecg.common.email.EmailServiceManager.getInstance;
|
import static org.jeecg.common.email.EmailServiceManager.getInstance;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -26,16 +33,45 @@ public class EmailPushMsgHandle implements ISendMsgHandle {
|
||||||
@Autowired
|
@Autowired
|
||||||
private ISysMessageService sysMessageService;
|
private ISysMessageService sysMessageService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SystemSendMsgHandle systemSendMsg;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendMessage(MessageDTO messageDTO) {
|
public void sendMessage(MessageDTO messageDTO) {
|
||||||
// 获取邮件发送服务器信息
|
// 获取邮件发送服务器信息
|
||||||
SysEmail sysEmail = abnormalAlarmClient.getSender().getResult();
|
SysEmail sysEmail = abnormalAlarmClient.getSender().getResult();
|
||||||
|
|
||||||
// 初始化邮件服务
|
// 初始化邮件服务
|
||||||
EmailServiceManager emailService = getInstance();
|
EmailServiceManager emailService = getInstance();
|
||||||
emailService.init(sysEmail);
|
emailService.init(sysEmail);
|
||||||
// 发送邮件
|
// 单独给每个用户发送邮件
|
||||||
emailService.sendMail(messageDTO);
|
/*String toUser = messageDTO.getToUser();
|
||||||
|
List<String> toUsers = StrUtil.splitTrim(toUser, ",");*/
|
||||||
|
String title = messageDTO.getTitle();
|
||||||
|
String context = messageDTO.getContent();
|
||||||
|
String alarmTitle = "邮件发送失败提醒";
|
||||||
|
String alarmContext = "系统给您发送的邮件[标题:"+ title +
|
||||||
|
", 内容:" + context + "]发送失败,失败原因:";
|
||||||
|
// 循环给每一个用户发送邮件
|
||||||
|
for (Map.Entry<String, String> userEmails : messageDTO.getUserEmail().entrySet()) {
|
||||||
|
String username = userEmails.getKey();
|
||||||
|
String email = userEmails.getValue();
|
||||||
|
try {
|
||||||
|
messageDTO.setToUser(email);
|
||||||
|
emailService.sendMailToOne(messageDTO);
|
||||||
|
} catch (MessagingException e) {
|
||||||
|
if (e instanceof SMTPAddressFailedException) { // 无效的电子邮箱异常
|
||||||
|
alarmContext += "无效的电子邮箱 " + email;
|
||||||
|
} else if (e instanceof SendFailedException) { // 邮件发送失败异常
|
||||||
|
alarmContext += e.getMessage();
|
||||||
|
}
|
||||||
|
// 邮件发送失败 给用户发送站内提示消息
|
||||||
|
MessageDTO sysMsg = new MessageDTO();
|
||||||
|
sysMsg.setToUser(username);
|
||||||
|
sysMsg.setTitle(alarmTitle);
|
||||||
|
sysMsg.setContent(alarmContext);
|
||||||
|
systemSendMsg.sendMessage(sysMsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sysMessageService.add(messageDTO);
|
sysMessageService.add(messageDTO);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user