【问题标题】:XPages: Copy MIME multiparts from mail to another Richtext documentXPages:将 MIME 多部分从邮件复制到另一个 Richtext 文档
【发布时间】:2017-12-19 13:56:34
【问题描述】:

尝试通过将邮件正文保存到富文本项中来从电子邮件创建新的 XPages 文档,我的文档已正确创建,附件正文也已创建,但文档中的所有嵌入图像都已替换为 imagePlace 持有者,下面是创建附件和图像的方法

private static void parseMimeEntity(RichTextItem attachmentBody, MIMEEntity entity,Session session,File tmpFolder,String fileSeparator,long attachmentNumber) {
    MIMEEntity child;
        try{        
            if(!entity.getContentType().equalsIgnoreCase("text")){
                String filename = null; 
                MIMEHeader header = null;
                header = entity.getNthHeader("Content-Disposition");    
                if (header != null) {
                    filename = header.getParamVal("filename");
                    filename = filename.replace("\"", "");
                    if ("".equals(filename)) filename = null;
                }
                if (filename == null) {
                    // when filename is null
                    filename = "Attachment" + attachmentNumber++ + ".txt";
                }
                String contentDisposition = entity.getNthHeader("Content-Disposition").getHeaderVal();

                if(contentDisposition.equalsIgnoreCase("inline")){                      
                    String contentType = entity.getNthHeader("Content-Type").getHeaderVal();                        
                    Stream stream = session.createStream();
                    if (stream.open(file.getAbsolutePath(), "binary")) {
                        entity.setContentFromBytes(stream, contentType, MIMEEntity.ENC_IDENTITY_BINARY);                        
                        stream.close();
                    }
                }else{

                Stream stream = session.createStream();
                if (stream.open(file.getAbsolutePath(), "binary")) {
                    entity.getContentAsBytes(stream);                           
                    stream.close();
                }                   
                attachmentBody.embedObject(EmbeddedObject.EMBED_ATTACHMENT, "", file.getAbsolutePath(), filename);      
                }   
                file.delete();
            }
        }
        catch(Exception ex){                
            System.out.println("NO FILE");
        }
        child = entity.getFirstChildEntity();
        if (child != null) {
            parseMimeEntity(attachmentBody, child,session,tmpFolder,fileSeparator,attachmentNumber);
        }
        child = entity.getNextSibling();
        if (child != null) {
            parseMimeEntity(attachmentBody, child,session,tmpFolder,fileSeparator,attachmentNumber);
        }   
    }

【问题讨论】:

  • 您是否确保禁用 MIME 转换,例如在创建新文档之前设置 session.setConvertMime(false);
  • 是的,我确实 setConvertMime 为 false
  • 在文档源中,我可以看到嵌入的图像为 base64 编码但未显示在富文本字段中
  • 您是否将文档内容输出到 XPage 上的富文本控件?如果是这样,您可以检查显示的 html 代码并检查嵌入图像的内容 id 是否未正确解析,或者 base64 数据 uri 定义是否存在问题...
  • 是的,我将邮件内容输出为富文本格式,并且 html 代码包含 content-ID,如何正确解析 base64?

标签: java email xpages


【解决方案1】:

当您创建一个新的 mime 条目时,您会获得新的边界字符串,这些字符串用作嵌入图像的键。检查传入图像的src 属性以了解确切的格式。

您需要调整这些属性以使图像正确显示。清理 HTML 并不好玩,请从这篇文章中获得一些建议:

https://wissel.net/blog/2017/04/from-blogsphere-to-a-static-site-part-2-cleaning-up-the-html.html

最终(需要检查,不知道我的想法)您可以指定边界并为您保存 HTML 清理

【讨论】:

    【解决方案2】:

    一种解决方案是对嵌入图像进行 Base64 编码,而不是将嵌入图像添加为附件。在这里查看我的答案:https://stackoverflow.com/a/19328276/785061

    【讨论】:

    • 图像已经作为 Base64 存储在文档中,但是当我使用 maildocument.getEmbeddedImagesList("Body") 它返回空列表
    猜你喜欢
    • 1970-01-01
    • 2012-10-16
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 2019-04-06
    • 1970-01-01
    相关资源
    最近更新 更多