Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从java.mail获取HTML文本/纯文本_Java_Jakarta Mail - Fatal编程技术网

如何从java.mail获取HTML文本/纯文本

如何从java.mail获取HTML文本/纯文本,java,jakarta-mail,Java,Jakarta Mail,当我在contentText中阅读java.mail中的电子邮件正文时,我会得到第一个纯文本,然后是HTML文本。即,如果发送消息是 模拟模拟2 contentText将包含: 嘲弄 模拟模拟2 下面是我加载contentText的代码: public void setContentText(Multipart multipart) throws MessagingException, IOException { contentText =""; for (int i = 0;

当我在contentText中阅读java.mail中的电子邮件正文时,我会得到第一个纯文本,然后是HTML文本。即,如果发送消息是

模拟
模拟2

contentText将包含:

嘲弄 模拟
模拟2

下面是我加载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 ?
    }

}
public void setContentText(多部分多部分)抛出MessaginException、IOException{
contentText=“”;
对于(int i=0;i”,“”)
.replaceAll(“可能会有所帮助