我想使用 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)可以执行此操作,请提供示例或示例引用。
干杯 西蒙
最佳答案
使用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);
}
}
关于java - 使用 Java 直接从标准 RFC 邮件格式的文件系统读取/解析电子邮件/邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3470920/