小编use*_*209的帖子

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

当我在contentText中读取来自 java.mail 的电子邮件正文时,我首先得到纯文本,然后得到 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 = …
Run Code Online (Sandbox Code Playgroud)

java jakarta-mail

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

jakarta-mail ×1

java ×1