feat:解码Base64邮件内容保存为可处理.eml文件

This commit is contained in:
nieziyan 2024-04-03 16:46:34 +08:00
parent 5c181d60b1
commit 2d840bcd8b

View File

@ -1,9 +1,13 @@
package org.jeecg.common.email;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import com.google.common.collect.Lists;
import com.sun.mail.imap.IMAPStore;
import com.sun.mail.smtp.SMTPAddressFailedException;
@ -31,8 +35,11 @@ import javax.mail.search.SearchTerm;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.InflaterOutputStream;
/**
* 邮件服务管理器
@ -125,13 +132,14 @@ public class EmailServiceManager {
properties.put("mail.store.protocol", "imap");
properties.put("mail.imap.host", email.getEmailServerAddress());
properties.put("mail.imap.port",email.getPort());
if (email.getIsQiye() == 1) {
Integer isQiye = email.getIsQiye();
if (ObjectUtil.isNotNull(isQiye) && isQiye == 1) {
properties.put("mail.imap.ssl.enable", "true");
} else {
properties.put("mail.imap.ssl.enable", "false");
}
HashMap IAM = new HashMap();
HashMap<String, String> IAM = new HashMap<>();
//带上IMAP ID信息由key和value组成例如nameversionvendorsupport-email等
IAM.put("name","myname");
IAM.put("version","1.0.0");
@ -530,10 +538,10 @@ public class EmailServiceManager {
final String from = address.substring(0,address.indexOf(StringConstant.AT));
//获取主题
subject = MimeUtility.decodeText(message.getSubject());
if(subject.indexOf(StringConstant.SLASH) != -1){
if(subject.contains(StringConstant.SLASH)){
subject = StringUtils.replace(subject,StringConstant.SLASH,"");
}
if(subject.indexOf(StringConstant.COLON) != -1){
if(subject.contains(StringConstant.COLON)){
subject = StringUtils.replace(subject,StringConstant.COLON,"");
}
receivedDate = message.getReceivedDate();
@ -556,9 +564,36 @@ public class EmailServiceManager {
fileName.append(SAVE_EML_SUFFIX);
final String rootPath = spectrumPathProperties.getRootPath();
final String emlPath = spectrumPathProperties.getEmlPath();
emlFile = new File(rootPath+emlPath+File.separator+fileName);
message.writeTo(new FileOutputStream(emlFile));
emlFile = new File(rootPath + emlPath + File.separator + fileName);
/* 如果邮件内容经过Base64编码 需要解码后再生成.eml文件 否则直接生成.eml文件 */
List<String> specified = ListUtil.toList("");
if (CollUtil.contains(specified, from)){ // 来自指定邮箱 此邮箱对邮件内容进行了Base64编码
// 先将未解码的内容保存为.eml文件
FileUtil.writeFromStream(message.getInputStream(), emlFile);
// 对正文内容进行解码
String content = (String) message.getContent();
// String content = FileUtil.readUtf8String(emlFile);
List<String> contents = ListUtil.toList(StrUtil.split(content, '|', 2));
String md5 = contents.get(0);
content = contents.get(1);
if (StrUtil.isBlank(content)) return emlFile;
content = StrUtil.cleanBlank(content);
String md5Verified = MD5.create().digestHex(content);
// 如果md5验证失败 则不进行解码
if (!StrUtil.equals(md5, md5Verified)) return emlFile;
contents = ListUtil.toList(StrUtil.split(content, "|"));
String base64 = contents.get(contents.size() - 1);
// 解码Base64字符串 并用解码后的内容覆盖未解码内容
byte[] data = Base64.decode(base64);
try (OutputStream out = Files.newOutputStream(emlFile.toPath());
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(out)) {
inflaterOutputStream.write(data);
} catch (Exception e) {
log.error("Create the base64 decoded file[{}] error: {}", emlFile.getAbsolutePath(), e.getMessage());
}
} else { // 直接生成.eml文件
message.writeTo(Files.newOutputStream(emlFile.toPath()));
}
// int bufferSize = 1024 * 1024; // 1M
// InputStream inputStream = message.getInputStream();
// BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, bufferSize);
@ -582,7 +617,7 @@ public class EmailServiceManager {
log.error(errorMsg);
throw new DownloadEmailException(errorMsg);
}catch (Exception e) {
log.error("",e);
log.error("The email download failed: {}", e.getMessage());
}finally {
EmailLogEvent event = new EmailLogEvent(batchesCounter,Thread.currentThread().getId(),EmailLogManager.GS_TYPE_GET,status,EmailLogManager.GETIDEML,subject,DateUtils.formatDate(receivedDate,"yyyy-MM-dd HH:mm:ss:SSS"),
(Objects.isNull(emlFile)?" ":emlFile.getAbsolutePath()));