我正在尝试修改现有的MimeMessage正文部分。我想过滤某些链接。你们中的任何人都知道为什么即使 body 部位的内容接缝要被更改的消息还是与旧内容一起发送的吗?是否正在进行一些缓存?知道如何解决这个问题吗?

这是我的代码:

public void resend(InputStream data) throws Exception {
    Session mailSession = createMailSession();
    //mailSession.setDebug(true);

    Transport transport = mailSession.getTransport();
    MimeMessage message = new MimeMessage(mailSession, data);

    Object content = message.getContent();
    if (content.getClass().isAssignableFrom(MimeMultipart.class)) {
        MimeMultipart mimeMultipart = (MimeMultipart) content;

        for (int i = 0; i < mimeMultipart.getCount(); i++) {

            BodyPart bodyPart = mimeMultipart.getBodyPart(i);
            if (bodyPart.getContentType().startsWith("text/plain")) {
                String cnt = updateContent((String) bodyPart.getContent());
                System.out.println("ContentType = " + bodyPart.getContentType());
                System.out.println("Content = " + cnt);

                bodyPart.setContent(cnt, bodyPart.getContentType());
            } else if (bodyPart.getContentType().startsWith("text/html")) {
                String cnt = updateContent((String) bodyPart.getContent());
                System.out.println("ContentType = " + bodyPart.getContentType());
                System.out.println("Content = " + cnt);

                bodyPart.setContent(cnt, bodyPart.getContentType());
            }
        }
    } else {
        String cnt = updateContent((String) message.getContent());
        System.out.println("ContentType = " + message.getContentType());
        System.out.println("Content = " + cnt);

        message.setContent(cnt, message.getContentType());
    }

    transport.connect();
    transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
    transport.close();
}

private String updateContent(String cnt) {
    return cnt.replace("www.xyz.pl", "www.new-xyz.pl");
}

输入流“数据”包含原始消息。

有任何想法吗?

提前致谢....

最佳答案

您需要在MimeMessage上调用saveChanges()(据我所知就足够了),另请参见:api-doc MimeMessage#saveChanges():

10-08 12:20