gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:16:43 24 4
gpt4 key购买 nike

当我在 contentText 中阅读来自 java.mail 的电子邮件正文时,我首先得到纯文本,然后得到此 HTML 文本。 IE。如果发送消息是

<div><b>Mock</b><br />Mock 2</div>

contentText 将包含:

Mock Mock <div><b>Mock</b><br />Mock 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 ?
}

}

最佳答案

This JavaMail FAQ entry可能有帮助。

关于java - 如何从 java.mail 获取 HTML 文本/纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18985557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com