简易的java解析eml文件(包含正文中的图片及附件)
下班前,记录一下,方便以后自己回顾
直接上代码
解析特定存储路径下的所有eml文件,按发件人姓名+发件时间将附件存在对应的二级目录,另外将发件时间、发件邮箱、收件邮箱、正文、发件人姓名这些主要信息存入ES方便查询。关键代码如下:
/*
*author:fantabulous24
* @date 2021年1月4日 下午4:41
*/
public JSONObject ceshieml(String filePath) {
JSONObject result = new JSONObject();
List list = new ArrayList();
try {
list = searchFile(filePath,list);
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < list.size(); i++) {
try {
result = parserFile(list.get(i).toString());
final IndexResponse response = this.client.prepareIndex("indexName", "type")
.setSource(result, XContentType.JSON).get();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/*
*author:fantabulous24
* @date 2021年1月4日 下午4:41
*/
public static List searchFile(String emlPath,List result) throws Exception {
File file = new File(emlPath);
if (!file.isDirectory()) {
result.add(file.getAbsolutePath());
} else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(emlPath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
result.add(readfile.getAbsolutePath());
} else if (readfile.isDirectory()) {
searchFile(emlPath + "\\" + filelist[i],result);
}
}
}
return result;
}
/*
*author:fantabulous24
* @date 2021年1月4日 下午4:41
*/
/**
* 解析文件
*
* @param emlPath 文件路径
* */
public static JSONObject parserFile(String emlPath) throws Exception {
JSONObject result = new JSONObject();
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
InputStream inMsg;
inMsg = new FileInputStream(emlPath);
Message msg = new MimeMessage(session, inMsg);
result = parseEml(msg);
return result;
}
/*
*author:fantabulous24
* @date 2021年1月4日 下午4:41
*/
/**
* 解析文件
*
* @param emlPath 文件路径
* */
private static JSONObject parseEml(Message msg) throws Exception {
JSONObject result = new JSONObject();
// 发件人信息
Address[] froms = msg.getFrom();
if (froms != null) {
InternetAddress addr = (InternetAddress) froms[0];
result.put("发件人地址",addr.getAddress());
result.put("发件人姓名", addr.getPersonal());
}
//收件人信息
Address[] tos = msg.getAllRecipients();
List<String> sjrAddressList = new ArrayList<>();
for (Address a : tos) {
InternetAddress addr = (InternetAddress)a;
sjrAddressList.add(addr.getAddress());
}
result.put("收件人地址列表", sjrAddressList);
result.put("邮件主题", msg.getSubject());
String time = msg.getSentDate().toInstant().toString().substring(0,10).replace("-","");
result.put("发件时间", msg.getSentDate().toInstant().toString().substring(0,10).replace("-",""));
// getContent() 是获取包裹内容, Part相当于外包装
Object o = msg.getContent();
if (o instanceof Multipart) {
Multipart multipart = (Multipart) o;
InternetAddress addr = (InternetAddress) froms[0];
String name = addr.getAddress();
reMultipart(multipart,result,name,time);
} else if (o instanceof Part) {
Part part = (Part) o;
InternetAddress addr = (InternetAddress) froms[0];
String name = addr.getAddress();
result = rePart(part,result,name,time);
} else {
result.put("类型", msg.getContentType());
result.put("内容", msg.getContent());
}
System.out.println("拼出来的内容"+result);
return result;
}
/*
*author:fantabulous24
* @date 2021年1月4日 下午4:41
*/
/**
* 解析文件
*
* @param emlPath 文件路径
* */
/**
* 解析内容
*
* @param part
* @throws Exception
*/
private static JSONObject rePart(Part part,JSONObject result,String name,String time) throws Exception {
if (part.getDisposition() != null) {
String strFileNmae = part.getFileName();
if(strFileNmae != null) {
// MimeUtility.decodeText解决附件名乱码问题
strFileNmae= MimeUtility.decodeText(strFileNmae);
System.out.println("发现附件: "+ strFileNmae);
result.put("附件名字",strFileNmae);
File file=new File("路径" + name + "\\" +time);
if(!file.exists()) {//如果文件夹不存在
file.mkdirs();//创建文件夹
}
// 打开附件的输入流
InputStream in = part.getInputStream();
String strFile = "路径" + name + "\\" +time+ "\\" + strFileNmae;
FileOutputStream out = new FileOutputStream(strFile);
byte[] bytes = new byte[1024];
while(in.read(bytes,0,1024) != -1){
out.write(bytes);
}
in.close();
out.close();
}
System.out.println("附件内容:" + part.getContent());
} else {
if (part.getContentType().startsWith("text/plain")) {
result.put("邮件内容",part.getContent());
} else {
System.out.println("是否有图片"+part.getDataHandler().getContentType());
}
}
return result;
}
/*
*author:fantabulous24
* @date 2021年1月4日 下午4:41
*/
/**
* 解析文件
*
* @param emlPath 文件路径
* */
/**
* 解析内容
*
* @param part
* @throws Exception
*/
/**
* 接卸包裹(含所有邮件内容(包裹+正文+附件))
* @param multipart
* @throws Exception
*/
private static void reMultipart(Multipart multipart,JSONObject result,String name,String time) throws Exception {
// System.out.println("邮件共有" + multipart.getCount() + "部分组成");
// 依次处理各个部分
for (int j = 0, n = multipart.getCount(); j < n; j++) {
// System.out.println("处理第" + j + "部分");
Part part = multipart.getBodyPart(j);// 解包, 取出 MultiPart的各个部分,
// 每部分可能是邮件内容,
// 也可能是另一个小包裹(MultipPart)
// 判断此包裹内容是不是一个小包裹, 一般这一部分是 正文 Content-Type: multipart/alternative
if (part.getContent() instanceof Multipart) {
Multipart p = (Multipart) part.getContent();// 转成小包裹
// 递归迭代
reMultipart(p,result,name,time);
} else {
rePart(part,result,name,time);
}
}
}
另附一个链接,这位博主相关理论写的很详细,值得参考学习:https://www.cnblogs.com/huangminwen/p/6107078.html
如有不当之处,欢迎指正。您的指点是我进步的捷径。
友情提示,小哥哥,打赏功能要不要试一试。