feat(core):邮件支持附件发送

yun-zuoyi
汪云昊 5 years ago
parent 3c688739f0
commit b1787918b5

@ -15,6 +15,7 @@ import cn.estsh.i3plus.pojo.base.bean.BaseThreadLocal;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.model.common.CloudPagerModel;
import cn.estsh.i3plus.pojo.model.license.ImppLicense;
import cn.estsh.i3plus.pojo.model.platform.UserDetailModel;
import cn.estsh.i3plus.pojo.model.platform.UserDetailPagerModel;
@ -243,6 +244,9 @@ public class WhiteController extends CoreBaseController {
log.setLogLoginStatus(userLoginStatus);
log.setLogLoginBrowser(ServletRequestTool.getRequestBrowser(request));
log.setLogLoginHost(IpAddr);
if (StringUtils.isBlank(log.getLogLoginHost()) || "null".equals(log.getLogLoginHost())) {
log.setLogLoginHost(ServletRequestTool.getRequestHost(request));
}
log.setLogLoginPlatform(CommonEnumUtil.LOG_LOGIN_PLATFORM.descOf(ServletRequestTool.getRequestOs(request)));
log.setLogLoginDateTime(TimeTool.getNowTime(true));
@ -280,6 +284,23 @@ public class WhiteController extends CoreBaseController {
}
}
@PostMapping(value = "/sys-locale-resource/query")
@ApiOperation(value = "按条件查询资源")
public ResultBean findSysLocaleResource(@RequestBody CloudPagerModel<SysLocaleResource> pagerModel){
try {
ListPager<SysLocaleResource> resourceList = sysLocaleResourceService.querySysLocaleResourceByPager(
pagerModel.getObj(), pagerModel.getPager()
);
return ResultBean.success("操作成功")
.setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
.setListPager(resourceList);
}catch(ImppBusiException busExcep){
return ResultBean.fail(busExcep);
}catch(Exception e){
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
}
}
@PostMapping(value = "/find-resource")
@ApiOperation(value = "按条件查询资源")
public ResultBean<SysLocaleResource> findSysLocaleResource(@RequestBody SysLocaleResource resource){

@ -57,8 +57,8 @@ public class SysLogUserLoginController extends CoreBaseController {
try {
ListPager logUserLoginListPager = sysLogUserLoginService.querySysLogUserLoginByPager(sysLogUserLogin, pager);
return ResultBean.success("查询成功").setListPager(logUserLoginListPager).setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
}catch(ImppBusiException busExcep){
return ResultBean.fail(busExcep);
}catch(ImppBusiException e){
return ResultBean.fail(e);
}catch(Exception e){
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
}
@ -75,14 +75,14 @@ public class SysLogUserLoginController extends CoreBaseController {
String fileName = "log-user-login-" + System.currentTimeMillis() + ".xls";
file = new File(fileName);
file.createNewFile();
File excle = excelTool.exportData(file, list, SysLogUserLogin.class, (LinkedHashMap<String, String>) ExcelTool.getColName(SysLogUserLogin.class));
File excel = excelTool.exportData(file, list, SysLogUserLogin.class, (LinkedHashMap<String, String>) ExcelTool.getColName(SysLogUserLogin.class));
response.setContentType("application/force-download"); // 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName); // 设置文件名
response.addHeader("Content-type", FileContentTypeTool.getContentType(StringTool.getStringFileSuffix(fileName,true)));
// 设置文件名
try (BufferedInputStream bis = new BufferedInputStream(new DataInputStream(new FileInputStream(excle)))) {
try (BufferedInputStream bis = new BufferedInputStream(new DataInputStream(new FileInputStream(excel)))) {
OutputStream os = response.getOutputStream();
byte[] buffer = new byte[1024];
int i = bis.read(buffer);

@ -40,6 +40,8 @@ import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import static cn.estsh.i3plus.platform.common.util.CommonConstWords.REDIS_PREFIX_LOCK_GET_ORDER_NO;
/**
* @Description :
* @Reference :
@ -152,8 +154,14 @@ public class SysOrderNoRuleController extends CoreBaseController {
public ResultBean deleteSysOrderNoRuleById(@PathVariable("id") String idStr) {
try {
ValidatorBean.checkNotNull(idStr, "id不能为空");
SysOrderNoRule sysOrderNoRule = sysOrderNoRuleService.getSysOrderNoRuleById(Long.parseLong(idStr));
ValidatorBean.checkNotNull(sysOrderNoRule,"单号规则不存在");
// 清理缓存
redisRes.deleteKey(REDIS_PREFIX_LOCK_GET_ORDER_NO + ":" + sysOrderNoRule.getOrderNoRuleCode());
sysOrderNoRuleService.deleteSysOrderNoRuleById(Long.parseLong(idStr));
// 删除
sysOrderNoRuleService.deleteSysOrderNoRuleById(sysOrderNoRule.getId());
return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
} catch (ImppBusiException busExcep) {
return ResultBean.fail(busExcep);

@ -1,5 +1,7 @@
package cn.estsh.i3plus.core.apiservice.mq;
import cn.estsh.i3plus.core.api.iservice.busi.ISysFileAttachService;
import cn.estsh.i3plus.core.api.iservice.busi.ISysFileService;
import cn.estsh.i3plus.core.api.iservice.busi.ISysMessageService;
import cn.estsh.i3plus.core.api.iservice.busi.ISysUserService;
import cn.estsh.i3plus.core.apiservice.util.MailUtil;
@ -8,12 +10,12 @@ import cn.estsh.i3plus.platform.common.tool.JsonUtilTool;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.ImppEnumUtil;
import cn.estsh.i3plus.pojo.platform.bean.SysMessage;
import cn.estsh.i3plus.pojo.platform.bean.SysRefUserMessage;
import cn.estsh.i3plus.pojo.platform.bean.SysUser;
import cn.estsh.i3plus.pojo.platform.bean.*;
import cn.estsh.impp.framework.boot.fileservice.ImppFileService;
import cn.estsh.impp.framework.boot.util.RedisCacheTool;
import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
@ -25,6 +27,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.io.InputStream;
/**
* @Description :
@ -40,11 +43,22 @@ public class MessageMailQueueReceiver {
@Autowired
private ISysMessageService sysMessageService;
@Autowired
private ISysUserService sysUserService;
@Autowired
private MailUtil mailUtil;
@Autowired
private ISysFileAttachService sysFileAttachService;
@Autowired
private ISysFileService sysFileService;
@Autowired
private ImppFileService fileService;
/**
*
*
@ -55,7 +69,7 @@ public class MessageMailQueueReceiver {
@RabbitListener(queues = PlatformConstWords.QUEUE_IMPP_MESSAGE_MAIL)
public void processImppMail(String data, Channel channel, Message message) {
SysMessage msg = JsonUtilTool.decode(data, SysMessage.class);
Long startTime = System.currentTimeMillis();
long startTime = System.currentTimeMillis();
try {
LOGGER.debug("【MQ-QUEUE_IMPP_MESSAGE_MAIL】数据接收成功{}", msg);
if (msg.getMessageSendTime() == null) {
@ -70,24 +84,42 @@ public class MessageMailQueueReceiver {
// 默认发送成功
msg.setSendStatus(ImppEnumUtil.MESSAGE_SEND_STATUS.SEND_SUCCESS.getValue());
// 设置邮件主题内容及收件人
// 初始化邮箱工具信息
mailUtil.init();
// 设置邮件主题内容及收件人
mailUtil.setSubject(msg.getMessageTitle());
mailUtil.setContentType(ImppEnumUtil.MESSAGE_TYPE_CONTENT.valueOfDescription(msg.getMessageContentType()));
mailUtil.setBody(msg.getMessageContent());
// 判断收件人类型
if (msg.getMessageReceiverType() != null) {
if (msg.getMessageReceiverType().intValue() == ImppEnumUtil.MESSAGE_RECEIVER_TYPE.URGENT.getValue()) {
// 判断是否为系统紧急提示 微服注册状态提示
mailUtil.setTo(RedisCacheTool.getSysConfigStrVal(PlatformConstWords.CONTACT_MAIL));
// 绑定附件信息
if (msg.getFileAttachIdList() != null && msg.getFileAttachIdList().size() != 0) {
// 附件id
InputStream in = null;
for (Long attachId : msg.getFileAttachIdList()) {
SysFile sysFile = sysFileService.getSysFileById(attachId);
try {
mailUtil.send();
} catch (Exception e) {
saveSendErrorMsg(msg, RedisCacheTool.getSysConfigStrVal(PlatformConstWords.CONTACT_MAIL), e);
in = fileService.downFile(sysFile);
mailUtil.addAttachment(sysFile.getFileOriginalName(), IOUtils.toByteArray(in));
SysFileAttach sysFileAttach = new SysFileAttach();
sysFileAttach.setFileId(attachId);
sysFileAttach.setAttachSoftType(msg.getMessageSoftType());
sysFileAttach.setAttachSource(ImppEnumUtil.FILE_ATTACH_SOURCE.MAIL.getValue());
sysFileAttach.setFileNameRdd(sysFile.getFileOriginalName());
sysFileAttach.setFilePathRdd(sysFile.getFilePath());
sysFileAttachService.insertSysFileAttach(sysFileAttach);
}finally {
if(in != null) {
in.close();
}
}
}
}
} else if (msg.getMessageReceiverType().intValue() == ImppEnumUtil.MESSAGE_RECEIVER_TYPE.EXTERNAL.getValue()) {
// 判断收件人类型
if (msg.getMessageReceiverType() != null) {
if (msg.getMessageReceiverType() == ImppEnumUtil.MESSAGE_RECEIVER_TYPE.EXTERNAL.getValue()) {
//判断是否为外部邮件
mailUtil.setTo(StringUtils.split(msg.getMessageReceiversNameRdd(), ","));
try {
@ -96,15 +128,15 @@ public class MessageMailQueueReceiver {
saveSendErrorMsg(msg, RedisCacheTool.getSysConfigStrVal(PlatformConstWords.CONTACT_MAIL), e);
}
} else if (msg.getMessageReceiverType().intValue() == ImppEnumUtil.MESSAGE_RECEIVER_TYPE.INTERNAL.getValue()) {
} else if (msg.getMessageReceiverType() == ImppEnumUtil.MESSAGE_RECEIVER_TYPE.INTERNAL.getValue()) {
// 收件人信息
String[] messageReceiver = msg.getMessageReceiversId().split(",");
SysRefUserMessage refUserMessage;
SysUser sysUser;
for (int i = 0; i < messageReceiver.length; i++) {
sysUser = sysUserService.getSysUserById(Long.parseLong(messageReceiver[i]));
for (String receiver : messageReceiver) {
sysUser = sysUserService.getSysUserById(Long.parseLong(receiver));
refUserMessage = new SysRefUserMessage();
refUserMessage.setMessageId(msg.getId());
@ -128,6 +160,8 @@ public class MessageMailQueueReceiver {
saveSendErrorMsg(msg, RedisCacheTool.getSysConfigStrVal(PlatformConstWords.CONTACT_MAIL), e);
}
}
mailUtil.cleanAttachmentList();
}
}

@ -1,9 +1,12 @@
package cn.estsh.i3plus.core.apiservice.util;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.tool.StringTool;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.platform.common.util.FileContentTypeTool;
import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ImppEnumUtil;
import cn.estsh.i3plus.pojo.platform.bean.MailConfig;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ImppRedis;
@ -12,18 +15,19 @@ import com.sun.mail.smtp.SMTPAddressFailedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.annotation.Resource;
import javax.mail.Address;
import javax.mail.AuthenticationFailedException;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.util.ByteArrayDataSource;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
@ -52,6 +56,7 @@ public class MailUtil {
private String smtpUser = "";
private String smtpPassword = "";
private boolean isAuthenticationSMTP = false;
private final List<BodyPart> attachmentList = new ArrayList<>();
@Resource(name = CommonConstWords.IMPP_REDIS_RES)
private ImppRedis redisRes;
@ -73,6 +78,7 @@ public class MailUtil {
this.smtpUser = RedisCacheTool.getSysConfigStrVal(PlatformConstWords.MAIL_USER);
this.smtpPassword = RedisCacheTool.getSysConfigStrVal(PlatformConstWords.MAIL_PASSWORD);
this.isAuthenticationSMTP = true;
this.attachmentList.clear();
return this;
} catch (Exception e) {
LOGGER.error("邮箱配置初始化失败!", e.getMessage());
@ -201,6 +207,49 @@ public class MailUtil {
}
/**
*
* @param fileName
* @param attachmentByte
* @throws MessagingException
*/
public void addAttachment(String fileName, byte[] attachmentByte) throws MessagingException {
addAttachment(fileName, attachmentByte,
FileContentTypeTool.getContentType(StringTool.getStringFileSuffix(fileName, true)));
}
/**
*
* @param fileName
* @param attachmentByte
* @param contentType
* @throws MessagingException
*/
public void addAttachment(String fileName, byte[] attachmentByte,String contentType) throws MessagingException {
BodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new ByteArrayDataSource(attachmentByte, contentType);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(fileName);
attachmentList.add(attachmentBodyPart);
}
/**
*
* @param attachment
* @throws MessagingException
*/
public void addAttachment(File attachment) throws MessagingException {
BodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(attachment.getName());
attachmentList.add(attachmentBodyPart);
}
public void cleanAttachmentList(){
attachmentList.clear();
}
/**
*
*/
public void send() {
@ -241,7 +290,23 @@ public class MailUtil {
// 消息主体内容 处理
this.content = this.content == null ? "" : this.content;
msg.setContent(this.content, this.contentType);
// 判断是否存在附件
if (!attachmentList.isEmpty()) {
// 添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart();
// 添加邮件正文
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(content, this.contentType);
multipart.addBodyPart(contentPart);
for (BodyPart bodyPart : attachmentList) {
multipart.addBodyPart(bodyPart);
}
msg.setContent(multipart);
} else {
msg.setContent(this.content, this.contentType);
}
msg.saveChanges();
if (this.isAuthenticationSMTP) {
@ -255,7 +320,7 @@ public class MailUtil {
LOGGER.info("邮件发送成功");
} catch (SMTPAddressFailedException e) {
LOGGER.error(e.getClass() + "\t" + e.getMessage());
LOGGER.error("邮件发送异常",e);
throw ImppExceptionBuilder.newInstance()
.setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
.setErrorCode(ImppExceptionEnum.MAIL_RECIPIENT_NOT_EXIST.getCode())
@ -263,7 +328,7 @@ public class MailUtil {
.setErrorSolution("请重新设置收件人")
.build();
} catch (AuthenticationFailedException e) {
LOGGER.error(e.getClass() + "\t" + e.getMessage());
LOGGER.error("邮件发送异常",e);
throw ImppExceptionBuilder.newInstance()
.setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
.setErrorCode(ImppExceptionEnum.MAIL_LOGIN_PASS_ERROR.getCode())
@ -271,7 +336,7 @@ public class MailUtil {
.setErrorSolution("请重新设置账号或密码")
.build();
} catch (MessagingException e) {
LOGGER.error(e.getClass() + "\t" + e.getMessage());
LOGGER.error("邮件发送异常",e);
throw ImppExceptionBuilder.newInstance()
.setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
.setErrorCode(ImppExceptionEnum.MAIL_SERVER_CONFIG_ERROR.getCode())
@ -279,7 +344,7 @@ public class MailUtil {
.setErrorSolution("请重新设置邮件服务器地址或端口")
.build();
} catch (UnsupportedEncodingException e) {
LOGGER.error(e.getClass() + "\t" + e.getMessage());
LOGGER.error("邮件发送异常",e);
throw ImppExceptionBuilder.newInstance()
.setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
.setErrorCode(ImppExceptionEnum.MAIL_SERVER_CONFIG_ERROR.getCode())
@ -287,7 +352,7 @@ public class MailUtil {
.setErrorSolution("请重新设置邮件服务器地址或端口")
.build();
} catch (Exception e) {
LOGGER.error(e.getClass() + "\t" + e.getMessage());
LOGGER.error("邮件发送异常",e);
throw ImppExceptionBuilder.newInstance()
.setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
.setErrorCode(ImppExceptionEnum.MAIL_SERVER_CONFIG_ERROR.getCode())
@ -296,4 +361,25 @@ public class MailUtil {
.build();
}
}
public static void main(String[] args) throws IOException, MessagingException {
MailConfig mailConfig = new MailConfig();
mailConfig.setMailSwitch(1);
mailConfig.setMailHost("smtphm.qiye.163.com");
mailConfig.setMailPort(25);
mailConfig.setMailNick("impp-test@estsh.com");
mailConfig.setMailUser("impp-test@estsh.com");
mailConfig.setMailPassword("Aa135790");
MailUtil mailUtil = new MailUtil();
mailUtil.init(mailConfig);
mailUtil.setNick("yunhao");
mailUtil.setTo("yunhaok@163.com");
mailUtil.setContentType(ImppEnumUtil.MESSAGE_TYPE_CONTENT.TEXT.getDescription());
mailUtil.setSubject("test");
mailUtil.setBody("666");
File file = new File("C:\\Users\\yunha\\Desktop\\miss_file (2).txt");
mailUtil.addAttachment(file);
mailUtil.send();
}
}

Loading…
Cancel
Save