如何修改现有的Java邮件MimeMessage正文部分?

发布于 2024-12-08 15:07:52 字数 2001 浏览 4 评论 0原文

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

这是我的代码:

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");
}

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

有什么想法吗?

提前致谢....

I am trying to modify existing MimeMessage body part. I would like to filter certain links. Does any of you know why even though body part content seams to be changed message is sent with old content? Is there some caching going on? Any idea how to solve this?

Here is my code:

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");
}

Input stream "data" contains raw message.

Any ideas?

Thanks in advance....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深居我梦 2024-12-15 15:07:53

为了更新 text/plain 和 text/html 部分,我使用了 Jsoup 提供的功能

     MimeMessage message = new MimeMessage(mailSession, data);
     String newText ="Whatever you want";
     updateText(message);
     message.saveChanges();


 private void updateText(String newText, MimePart part){

     if the mime type is "text/plain"{
            part.setText(newText, "UTF-8");
     }else if the mime type is "text/html"{
           String html = (String) part.getContent();
           Document document = Jsoup.parse(html)
           Element body = doc.body();
           body.text(newText);
           part.setContent(doc.html(), "text/html;charset=UTF-8");
     }else if the mime type is multipart/*{
           Multipart multi = (Multipart) part.getContent();
           int count = multi.getCount();
           for (int i = 0; i < count; i++) {
               updateText(newText, multi.getbodyPart(i);
           }
     }
 }

To update both the text/plain and text/html sections, I used functionalities provided by Jsoup

     MimeMessage message = new MimeMessage(mailSession, data);
     String newText ="Whatever you want";
     updateText(message);
     message.saveChanges();


 private void updateText(String newText, MimePart part){

     if the mime type is "text/plain"{
            part.setText(newText, "UTF-8");
     }else if the mime type is "text/html"{
           String html = (String) part.getContent();
           Document document = Jsoup.parse(html)
           Element body = doc.body();
           body.text(newText);
           part.setContent(doc.html(), "text/html;charset=UTF-8");
     }else if the mime type is multipart/*{
           Multipart multi = (Multipart) part.getContent();
           int count = multi.getCount();
           for (int i = 0; i < count; i++) {
               updateText(newText, multi.getbodyPart(i);
           }
     }
 }
守不住的情 2024-12-15 15:07:52

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

更新此消息的相应标头字段,使其与消息的内容一致。如果此消息包含在文件夹中,则对此消息所做的任何更改都会提交到包含的文件夹。

如果消息标头或内容的任何部分发生更改,则必须调用 saveChanges 以确保这些更改是永久性的。否则,任何此类修改可能会也可能不会保存,具体取决于文件夹实现。

You need to call saveChanges() on the MimeMessage (which as far as I know should be sufficient), see also: api-doc MimeMessage#saveChanges():

Updates the appropriate header fields of this message to be consistent with the message's contents. If this message is contained in a Folder, any changes made to this message are committed to the containing folder.

If any part of a message's headers or contents are changed, saveChanges must be called to ensure that those changes are permanent. Otherwise, any such modifications may or may not be saved, depending on the folder implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文