Java 电子邮件消息解析器?

发布于 2024-09-13 18:51:59 字数 903 浏览 7 评论 0原文

有人熟悉可以帮助解析下面电子邮件的字段(日期、主题、发件人、收件人)的 Java 库吗?

Message-ID: <19815303.1075861029555.JavaMail.ss@kk>
Date: Wed, 6 Mar 2010 12:32:20 -0800 (PST)
From: [email protected]
To: [email protected]
Subject: some subject
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-From: one, some <[email protected]>
X-To: one
X-cc: 
X-bcc: 
X-Folder: Bob\Inbox
X-Origin: Bob-R
X-FileName: rbob (Non-Privileged).pst


some message

Is anyone familiar with a Java library that helps with parsing the fields (date, subject, from, to) of the email below?

Message-ID: <19815303.1075861029555.JavaMail.ss@kk>
Date: Wed, 6 Mar 2010 12:32:20 -0800 (PST)
From: [email protected]
To: [email protected]
Subject: some subject
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-From: one, some <[email protected]>
X-To: one
X-cc: 
X-bcc: 
X-Folder: Bob\Inbox
X-Origin: Bob-R
X-FileName: rbob (Non-Privileged).pst


some message

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨小沫ゞ 2024-09-20 18:51:59

JavaMail 是一个 oracle 库,提供邮件服务和邮件相关服务(例如解析 javax.mail 包中的常规和 MIME 消息。此外,Apache 还有一个用于邮件处理的 Commons Email 库。

在 JavaMail api 中,解析包含电子邮件消息(可能是也可能不是显式 MIME)的字符串的简单方法如下,

String content = ...
Session s = Session.getInstance(new Properties());
InputStream is = new ByteArrayInputStream(content.getBytes());
MimeMessage message = new MimeMessage(s, is);

并且可以像这样解析标头

message.getAllHeaderLines();
for (Enumeration<Header> e = message.getAllHeaders(); e.hasMoreElements();) {
    Header h = e.nextElement();
    h.getName();
    h.getValue();
}

JavaMail is an oracle library that provides mail services and mail related services (like parsing conventional & MIME messages) in the javax.mail package. Additionally Apache has a Commons Email library for mail handling.

In the JavaMail api, a simple way to parse a string containing an email message (which may or may not be explicitly MIME) would be as follows

String content = ...
Session s = Session.getInstance(new Properties());
InputStream is = new ByteArrayInputStream(content.getBytes());
MimeMessage message = new MimeMessage(s, is);

and parsing the headers could be done like this

message.getAllHeaderLines();
for (Enumeration<Header> e = message.getAllHeaders(); e.hasMoreElements();) {
    Header h = e.nextElement();
    h.getName();
    h.getValue();
}
缱绻入梦 2024-09-20 18:51:59

我在使用 JavaMail 时遇到了问题(它无法解析一些它应该解析的电子邮件消息)。

我使用 Mime4J 获得了更好的结果。

I have had problems with JavaMail (it fails to parse some email messages that it should).

I have had much better results with Mime4J.

眼眸印温柔 2024-09-20 18:51:59

我建议您使用 email-mime-parser

以下示例代码给出您需要的所有相关信息:

ContentHandler contentHandler = new CustomContentHandler();

MimeConfig mime4jParserConfig = new MimeConfig();
BodyDescriptorBuilder bodyDescriptorBuilder = new DefaultBodyDescriptorBuilder();
MimeStreamParser mime4jParser = new MimeStreamParser(mime4jParserConfig,DecodeMonitor.SILENT,bodyDescriptorBuilder);
mime4jParser.setContentDecoding(true);
mime4jParser.setContentHandler(contentHandler);

InputStream mailIn = 'Provide email mime stream here';
mime4jParser.parse(mailIn);

Email email = ((CustomContentHandler) contentHandler).getEmail();

List<Attachment> attachments =  email.getAttachments();

Attachment calendar = email.getCalendarBody();
Attachment htmlBody = email.getHTMLEmailBody();
Attachment plainText = email.getPlainTextEmailBody();

String to = email.getToEmailHeaderValue();
String cc = email.getCCEmailHeaderValue();
String from = email.getFromEmailHeaderValue();

I would suggest you use email-mime-parser,

Following sample code gives you all the relevant info you need:

ContentHandler contentHandler = new CustomContentHandler();

MimeConfig mime4jParserConfig = new MimeConfig();
BodyDescriptorBuilder bodyDescriptorBuilder = new DefaultBodyDescriptorBuilder();
MimeStreamParser mime4jParser = new MimeStreamParser(mime4jParserConfig,DecodeMonitor.SILENT,bodyDescriptorBuilder);
mime4jParser.setContentDecoding(true);
mime4jParser.setContentHandler(contentHandler);

InputStream mailIn = 'Provide email mime stream here';
mime4jParser.parse(mailIn);

Email email = ((CustomContentHandler) contentHandler).getEmail();

List<Attachment> attachments =  email.getAttachments();

Attachment calendar = email.getCalendarBody();
Attachment htmlBody = email.getHTMLEmailBody();
Attachment plainText = email.getPlainTextEmailBody();

String to = email.getToEmailHeaderValue();
String cc = email.getCCEmailHeaderValue();
String from = email.getFromEmailHeaderValue();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文