直接从标准RFC邮件格式的文件系统中读取/解析电子邮件/邮件 [英] Reading / parsing email / mail with Java directly from a filesystem in standard RFC mail format

查看:268
本文介绍了直接从标准RFC邮件格式的文件系统中读取/解析电子邮件/邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用java(1.6)直接从文件系统解析电子邮件,电子邮件将在文件系统文件夹中,如

I want to parse email messages directly from the filesystem using java (1.6) the email messages will be in a filesystem folder like

abc-12345.msg
qwe-23456.msg

abc-12345.msg qwe-23456.msg

,将采用标准格式:例如:

and will be in a standard format: for example:

MIME-Version: 1.0
Sender: mr.sender@gmail.com
Received: by 10.239.173.80 with HTTP; Thu, 12 Aug 2010 11:30:50 -0700 (PDT)
Date: Thu, 12 Aug 2010 15:30:50 -0300
Delivered-To: mr.recipient@gmail.com
Message-ID: <AANLkTikA-RbE_revkimBCWC2fUrG=t4T47AXq5FTx0vd@mail.gmail.com>
Subject: =?ISO-8859-1?Q?Hello_With_Acc=E9=F1ts?=
From: Mr Sender <mr.sender@gmail.com>
To: Mr Recipient <mr.recipient@gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

This is a testing one

Accent characters:

=E9=F3=FA=F1

Email End

想要解析这个文件或使用一个现有的库解析文件,让我可以访问标题/ from / to / subject / body等等。

I want to parse this file or use an existing library to parse the file to give me access to the headers / from / to / subject / body and so forth.

文件都在本地文件系统上。我没有通过pop / imap连接到消息存储。对我来说,最简单,最直接的方法是什么?文件可能包含附件。

The files are all held on a local filesystem. I am no connecting to a message store through pop / imap. What is the easiest, most straightforward way for me to do this. The files may contain attachments.

任何建议都非常受欢迎。如果有一个现有的api(也许是javamail)可以做到这一点,你可以提供示例或参考例子。

Any suggestions are very welcome. If there is an existing api (perhaps javamail) that can do this could you please provide examples or references to examples.

Cheers
Simon

Cheers Simon

推荐答案

使用javamail api:

use the javamail api:

例如:

  File[] mailFiles = getPertinentMailFiles();
  String host = "host.com";
  java.util.Properties properties = System.getProperties();
  properties.setProperty("mail.smtp.host", host);
  Session session = Session.getDefaultInstance(properties);
  for (File tmpFile : mailFiles) {
     MimeMessage email = null;
     try {
        FileInputStream fis = new FileInputStream(tmpFile);
        email = new MimeMessage(session, fis);
        System.out.println("content type: " + email.getContentType());
        System.out.println("\nsubject: " + email.getSubject());
        System.out.println("\nrecipients: " + Arrays.asList(email.getRecipients(Message.RecipientType.TO))); 
     } catch (MessagingException e) {
        throw new IllegalStateException("illegal state issue", e);
     } catch (FileNotFoundException e) {
        throw new IllegalStateException("file not found issue issue: " + tmpFile.getAbsolutePath() , e); 
     }
  }

这篇关于直接从标准RFC邮件格式的文件系统中读取/解析电子邮件/邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆