如何从Java邮件中获取HTML文本/纯文本

3

当我从java.mail中读取电子邮件正文的contentText时,我首先得到纯文本,然后是HTML文本。例如,如果发送消息是:

<div><b>模拟</b><br />模拟2</div>

contentText将包含:

模拟 模拟 <div><b>模拟</b><br />模拟2</div>

下面是我的代码来加载contentText:

public void setContentText(Multipart multipart) throws MessagingException, IOException {
    contentText ="";

    for (int i = 0; i < multipart.getCount(); i++) {
        BodyPart bodyPart = multipart.getBodyPart(i);
        getBodyToStringPart(bodyPart);
    }
}

protected void getBodyToStringPart(BodyPart bodyPart) throws MessagingException, IOException {
    String disposition = bodyPart.getDisposition();

    if (!StringUtils.equalsIgnoreCase(disposition, "ATTACHMENT")) {
        if (bodyPart.getContent() instanceof BASE64DecoderStream
                && bodyPart.getHeader("Content-ID") != null) {
            BASE64DecoderStream base64DecoderStream = (BASE64DecoderStream) bodyPart
                    .getContent();
            byte[] byteArray = IOUtils.toByteArray(base64DecoderStream);
            byte[] encodeBase64 = Base64.encodeBase64(byteArray);

            this.contentText = this.contentText.replaceAll(
                    "cid:"
                            + bodyPart.getHeader("Content-ID")[0].replaceAll(">", "")
                                    .replaceAll("<", ""), "data:" + bodyPart.getContentType()
                            + ";base64," + new String(encodeBase64, "UTF-8"));

        } else if (bodyPart.getContent() instanceof MimeMultipart) {
            MimeMultipart mimeMultipart = (MimeMultipart) bodyPart.getContent();
            for (int j = 0; j < mimeMultipart.getCount(); j++) {
                getBodyToStringPart(mimeMultipart.getBodyPart(j));
            }
        } else {
            this.contentText += bodyPart.getContent() + "";
        }
    } else {
        // TODO: Do we need attachments ?
    }

}
1个回答

5
这篇JavaMail常见问题解答可能会有所帮助。

点击这里查看。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接