Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
803 views
in Technique[技术] by (71.8m points)

parsing - Java Email message Parser?

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: BobInbox
X-Origin: Bob-R
X-FileName: rbob (Non-Privileged).pst


some message
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...