使用javamail读取电子邮件的html正文
问题描述:
我正在尝试获取包含标签等的html电子邮件的内容。现在我的代码只返回文本。这是我的代码:
I am trying to get the contents of an html email including the tags etc. right now my code only returns the texts.this is my code:
Store store = session.getStore("pop3");
store.connect(host, username, passwoed);
Folder folder = store.getFolder("Inbox");
if (!folder.exists()) {
System.out.println("No INBOX...");
System.exit(0);
}
folder.open(Folder.READ_WRITE);
Message[] msg = folder.getMessages();
for (int i = msg.length - 1; i > 0; i--) {
String sent1 = df.format(sent);
sent1 = sent1.trim();
int index11 = sent1.indexOf(DateTime);
if (index11 != -1) {
String to = InternetAddress.toString(msg[i].getRecipients(Message.RecipientType.TO));
String s1 = "";
try {
Multipart multipart = (Multipart) msg[i].getContent();
for (int x = 0; x < multipart.getCount(); x++) {
BodyPart bodyPart = multipart.getBodyPart(x);
String disposition = bodyPart.getDisposition();
if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
DataHandler handler = bodyPart.getDataHandler();
s1 = (String) bodyPart.getContent();
} else {
s1 = (String) bodyPart.getContent();
}
}
}
}
任何帮助都将不胜感激。
any help would be appreciated.
答
您可以找到的邮件Content-Type:TEXT / HTML
,如下所示:
You can find a mail with Content-Type: TEXT/HTML
like this:
Object content = message.getContent();
if (content instanceof Multipart) {
Multipart mp = (Multipart) content;
for (int i = 0; i < mp.getCount(); i++) {
BodyPart bp = mp.getBodyPart(i);
if (Pattern
.compile(Pattern.quote("text/html"),
Pattern.CASE_INSENSITIVE)
.matcher(bp.getContentType()).find()) {
// found html part
System.out.println((String) bp.getContent());
} else {
// some other bodypart...
}
}
}
输出:
<H1>Hi there</H1><p>Bye.</p>