JAVA代码如何解析eml格式的邮件呢?
下文笔者讲述使用java代码解析eml格式的文件的方法分享,如下所示
实现思路: 使用javax.mail 和 apache中的commons-email 即可实现对eml格式文件的解析例:
---项目中添加依赖 <!-- apache文件操作库 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.11.0</version> </dependency> <!-- 邮件解析基本库 --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> <!-- apache对javax.mail库的封装 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> <version>1.5</version> </dependency> ---编写测试代码---- public class Test { public static void main(String[] args) { String dir = "D:/tmp/"; try(InputStream inputStream = new FileInputStream(dir+"test3.eml")) { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); MimeMessage msg = new MimeMessage(session, inputStream); MimeMessageParser parser = new MimeMessageParser(msg).parse(); //邮件唯一id String messageId = parser.getMimeMessage().getMessageID(); System.out.println(messageId); //发件人 String from = parser.getFrom(); System.out.println(from); //收件人列表 list<Address> toArray = parser.getTo(); System.out.println(toArray); //抄送人列表 List<Address> ccArray = parser.getCc(); System.out.println(ccArray); //密送人列表 List<Address> bccArray = parser.getBcc(); System.out.println(bccArray); //邮件发送时间 Date sendDate = parser.getMimeMessage().getSentDate(); System.out.println(sendDate); //邮件主题 String subject = parser.getSubject(); System.out.println(subject); //获取正文 String html = parser.getHtmlContent(); System.out.println(html); //所有文件,包括附件和正文中的图片等文件 List<DataSource> dataSources = parser.getAttachmentList(); //获取不到html内容时,则获取非html文本内容 if (html == null || html.length()==0) { String plain = parser.getPlainContent(); System.out.println(plain); }else { //获取正文中的图片等文件 Collection<String> contentIds = parser.getContentIds(); for (String contentId : contentIds) { DataSource contentFile = parser.findAttachmentByCid(contentId); dataSources.remove(contentFile); String name = contentFile.getName(); InputStream is = contentFile.getInputStream(); FileUtils.copyInputStreamToFile(is,new File(dir+name)); html = html.replace("cid:"+contentId,name); } FileUtils.writeStringToFile(new File(dir+"test.html"),html, Charset.defaultCharset()); } for (DataSource dataSource : dataSources) { String name = dataSource.getName(); System.out.println(name); InputStream is = dataSource.getInputStream(); FileUtils.copyInputStreamToFile(is,new File(dir+name)); } }catch (Exception e) { e.printStackTrace(); } } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。