java中的multipart_在java中解析Multipart /与Multipart/Alternative主体...

我从客户端收到电子邮件,他们在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”处停止,并且永远不会到达消息的文本.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值