我从客户端收到电子邮件,他们在multipart / mixed消息中嵌套了multipart / alternative消息.当我得到消息的正文时,它只返回multipart / alternative级别,而我真正想要的是包含在multipart / alternative中的text / html部分.
我查看了javax.mail的javadocs,我找不到一个简单的方法来获取bodypart的主体,它本身是一个multipart或跳过第一个multipart / mixed部分并进入multipart / alternative body来读取text / html和text / plain pieces.
电子邮件结构如下所示:
...
Content-Type: multipart/mixed;
boundary="----=_Part_19487_1145362154.1418138792683"
------=_Part_19487_1145362154.1418138792683
Content-Type: multipart/alternative;
boundary="----=_Part_19486_1391901275.1418138792683"
------=_Part_19486_1391901275.1418138792683
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=ISO-8859-1
...
------=_Part_19486_1391901275.1418138792683
Content-Transfer-Encoding: 7bit
Content-Type: text/html; charset=ISO-8859-1
...
------=_Part_19486_1391901275.1418138792683--
------=_Part_19487_1145362154.1418138792683--
这是用于解析电子邮件的代码的大纲:
Message [] found = fldr.search(searchCondition);
for (int i = 0; i < found.length; i++) {
Message m = found[i];
Object o = m.getContent();
if (o instanceof Multipart) {
log.info("**This is a Multipart Message. ");
Multipart mp = (Multipart)o;
log.info("The Multipart message has " + mp.getCount() + " parts.");
for (int j = 0; j < mp.getCount(); j++) {
BodyPart b = mp.getBodyPart(j);
// Loop if the content type is multipart then get the content that is in that part,
// make it the new container and restart the loop in that part of the message.
if (b.getContentType().contains("multipart")) {
mp = (Multipart)b.getContent();
j = 0;
continue;
}
log.info("This content type is " + b.getContentType());
if(!b.getContentType().contains("text/html")) {
continue;
}
Object o2 = b.getContent();
if (o2 instanceof String) {
}
}
}
}
它似乎继续停留在第二个边界,而不是进一步解析任何东西.在上述消息的情况下,它在boundary =“—- = _ Part_19486_1391901275.1418138792683”处停止,并且永远不会到达消息的文本.