使用Java直接从标准RFC邮件格式的文件系统中读取/解析电子邮件/邮件

5
我希望能够使用Java(1.6版本)直接从文件系统解析电子邮件消息,这些邮件消息将位于文件系统文件夹中,例如:
abc-12345.msg qwe-23456.msg
它们的标准格式如下:
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

我希望能解析这个文件,或使用现有库来解析该文件,以便让我获取头部/发件人/收件人/主题/正文等内容。这些文件都保存在本地文件系统上。我没有通过pop / imap连接到消息存储区。对于我来说,最简单、最直接的方法是什么呢?这些文件可能包含附件。任何建议都非常欢迎。如果存在现有的API(例如javamail)可以实现此功能,请提供示例或引用示例。谢谢! - Simon
4个回答

7
使用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); 
     }
  }

2
如果您有电子邮件文本(即整个MIME有效负载),那么我建议使用 Mime4J 库进行处理。该库使用更加简单,速度更快,占用空间更小,比 "javax.mail" 更加优秀。

0

你可以立即使用JavaMail访问IMAP和POP3存储,但不能用于文件系统存储。

在JavaMail中,通过javax.mail.Store的实现来组织对电子邮件的访问。您可以查看JavaMail API-第三方产品以获取特定文件格式的javax.mail.Store的实现。如果找不到合适的内容,您可以自己实现javax.mail.Store。使用您的Storage,您可以使用JavaMail的所有功能,例如附件处理、所有奇怪的MIME等。


0
    // process import emails
        Properties props = System.getProperties();
        Session session = Session.getDefaultInstance(props, null);
        Store store = null;
        Folder folder = null;

         File tmpFile = new File("/Users/mugeeshhusain/servers/153232448813.3926.0");
            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);
            }
            boolean success = processMethod(email);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接