我必须像这样解析电子邮件并找出正文、日期等。任何帮助将不胜感激。
Date: XXXXX
Reply-To: XXXXX
Sender: XXXXX
From: XXXX
Subject: Test
In-Reply-To: XXXX
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="=__Part416ABDAE.0__="
=__Part416ABDAE.0__=
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: quoted-printable
Test
最佳答案
这是一个正则表达式解决方案:
import java.util.regex.*;
class Test {
public static void main(String[] args) {
String input = "Date: XXXXX\n"
+ "Reply-To: XXXXX\n"
+ "Sender: XXXXX\n"
+ "From: XXXX\n"
+ "Subject: Test\n"
+ "In-Reply-To: XXXX\n"
+ "Mime-Version: 1.0\n"
+ "Content-Type: multipart/alternative; " +
"boundary=\"=__Part416ABDAE.0__=\"";
Pattern p = Pattern.compile("(.*?):\\s+(.*)");
Matcher m = p.matcher(input);
while (m.find()) {
System.out.println("Key: " + m.group(1));
System.out.println("Val: " + m.group(2));
System.out.println();
}
}
}
输出:
Key: Date
Val: XXXXX
Key: Reply-To
Val: XXXXX
Key: Sender
Val: XXXXX
Key: From
Val: XXXX
Key: Subject
Val: Test
Key: In-Reply-To
Val: XXXX
Key: Mime-Version
Val: 1.0
Key: Content-Type
Val: multipart/alternative; boundary="=__Part416ABDAE.0__="
关于Java 电子邮件解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7384056/