直接从标准 RFC 邮件格式的文件系统中使用 Java 读取/解析电子邮件/邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3470920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading / parsing email / mail with Java directly from a filesystem in standard RFC mail format
提问by Simon B
I want to parse email messages directly from the filesystem using java (1.6) the email messages will be in a filesystem folder like
我想使用 java (1.6) 直接从文件系统解析电子邮件消息,电子邮件消息将位于文件系统文件夹中,例如
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: [email protected]
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: [email protected]
Message-ID: <[email protected]>
Subject: =?ISO-8859-1?Q?Hello_With_Acc=E9=F1ts?=
From: Mr Sender <[email protected]>
To: Mr Recipient <[email protected]>
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
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.
我想解析这个文件或使用现有的库来解析该文件,以便我可以访问标题/从/到/主题/正文等。
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.
这些文件都保存在本地文件系统上。我没有通过 pop / imap 连接到消息存储。对我来说,最简单、最直接的方法是什么?这些文件可能包含附件。
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.
任何建议都非常受欢迎。如果有可以执行此操作的现有 api(可能是 javamail),请提供示例或示例参考。
Cheers Simon
干杯西蒙
回答by Simon B
use the javamail api:
使用 javamail api:
for example:
例如:
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);
}
}
回答by Adam Gent
回答by Michael Konietzka
You can use JavaMail immediatly for accessing IMAP and POP3-Stores, not for filesystem storage.
您可以立即使用 JavaMail 来访问 IMAP 和 POP3 存储,而不是用于文件系统存储。
In JavaMail the access to email is organzied via implementations of javax.mail.Store. You can check JavaMail API - Third Party Productsfor implementations of javax.mail.Store for your special file format. Maybe you will implement javax.mail.Store by yourself, if you cannot find something appropriate. With using your Store, you can use all of the features of JavaMail eg attachment handling, all weird MIME stuff etc.
在 JavaMail 中,对电子邮件的访问是通过 javax.mail.Store 的实现来组织的。您可以查看JavaMail API - 第三方产品,了解您的特殊文件格式的 javax.mail.Store 实现。如果你找不到合适的东西,也许你会自己实现 javax.mail.Store 。通过使用您的商店,您可以使用 JavaMail 的所有功能,例如附件处理、所有奇怪的 MIME 内容等。

