posts - 145,comments - 23,views - 73万

在对eml文件进行索引的时候需要先对其进行解析,提取出其中的收件人、发件人、文件内容和附件等信息

下边是解析eml文件的一个demo(在运行之前需要先导入mail.jar 和commons-email.jar)

复制代码
 1 package com.jiaoyiping.windowsfilesearch.test;
 2 
 3 import org.apache.commons.mail.util.MimeMessageParser;
 4 import org.apache.commons.mail.util.MimeMessageUtils;
 5 
 6 import javax.mail.MessagingException;
 7 import javax.mail.internet.MimeMessage;
 8 import java.io.File;
 9 import java.io.IOException;
10 
11 /**
12  * Created with IntelliJ IDEA.
13  * User: 一平
14  * Date: 14-5-30
15  * Time: 上午9:09
16  * To change this template use File | Settings | File Templates.
17  */
18 public class EmailTest {
19     public static void main(String[] args) {
20         File file = new File("D:\\2013年12月21日重装系统之前数据备份\\邮件备份\\Fw_ Fw_ Fw_ 信息化报表需求.eml");
21         try {
22             MimeMessage mimeMessage = MimeMessageUtils.createMimeMessage(null, file);
23             MimeMessageParser parser = new MimeMessageParser(mimeMessage);
24             System.out.println("发件人:"+parser.getFrom());
25             System.out.println("主题:"+parser.getSubject());
26             System.out.println("收件人:"+parser.getTo());
27             System.out.println(parser.parse().getPlainContent());  //结果为空
28             System.out.println(parser.parse().getHtmlContent()); //结果为空
29             //parse.parse()方法返回的也是MimeMessageParser对象,不调用parse()方法就无法的到邮件内容,只能得到主题和收件人等信息
30             if(parser.parse().hasPlainContent()){  //这里一定要调用parser.parse方法
31                 System.out.println(parser.parse().getPlainContent());
32             }
33 
34 
35 
36         } catch (MessagingException e) {
37             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
38         } catch (IOException e) {
39             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
40         } catch (Exception e) {
41             e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
42         }
43 
44 
45     }
46 }
复制代码

 

posted on   梦中彩虹  阅读(3836)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示