【问题标题】:Extracting attachments from Outlook .msg files using ColdFusion使用 ColdFusion 从 Outlook .msg 文件中提取附件
【发布时间】:2017-12-11 02:23:43
【问题描述】:

我正在构建一个系统,允许 Intranet 用户将文件拖放到我们 ColdFusion 站点上的 div 中,经过一些验证后,该系统会自动将它们上传到文件服务器。我的要求之一是:当上传的文件是 .msg 文件(Outlook 电子邮件)时,提取作为该电子邮件附件的所有文件并单独上传。这可以使用org.apache.poi.hsmf.MAPIMessage Java 对象来实现。 使用以下代码,我可以看到列出的每个附件对象。然后我可以获取他们的文件名和扩展名并将每个文件保存到本地文件系统。

但是,如果附件是另一个 .msg 文件,这将不起作用。当我在附加的.msg 文件上调用getEmbeddedAttachmentObject() 时,它返回一个仅包含“未定义”的对象。非.msg 文件返回一个二进制对象,然后我可以将其传递给FileWrite() ColdFusion 函数。对MAPIMessage 对象的进一步检查表明它有一个write() 方法,但在调用它时我收到一条错误消息:

注意 - 此文件格式尚不支持写入,抱歉。

http://poi.apache.org 上的文档也支持这一点。

总而言之,我可以毫无问题地将每个电子邮件附件写入文件系统,除非附件是另一封电子邮件。是我运气不好还是有其他方法可以做到这一点?

<cfscript>
  // Load test .msg into MAPIMessage object
  MAPIMessage = createObject("java", "org.apache.poi.hsmf.MAPIMessage");
  message = MAPIMessage.init('C:\Test\Test Email 1 Attachment.msg');

  // Get array of attached files
  attachments = message.getAttachmentFiles();

  // If attachments were found
  if(arrayLen(attachments) > 0) {

    // Loop over each attachment
    for (i=1; i LTE arrayLen(attachments); i++) {

      // Dump the current attachment object
      writeDump( attachments[i] );

      // Get current attachment's binary data
      local.data=attachments[i].getEmbeddedAttachmentObject();

      // Dump binary data
      writeDump( local.data );

      // Get attachment's filename and extension
      attachmentFileName = attachments[i].attachLongFileName.toString();
      attachmentExtension = attachments[i].attachExtension.toString();

      // Dump filename and extension
      writeDump( attachmentFileName );
      writeDump( attachmentExtension );

      // Write attachment to local file system     
FileWrite("#expandPath('/')##attachments[i].attachLongFileName.toString()#", local.data);

   }
  }
</cfscript>

【问题讨论】:

    标签: java email coldfusion outlook apache-poi


    【解决方案1】:

    经过大量研究,我找到了解决问题的方法。由于尚未实现 write() 方法,我无法使用 ColdFusion 附带的 org.apache.poi.hsmf.MAPIMessage java 对象保存嵌入式 msg 文件。相反,我使用了一个名为Aspose.Email for Java 的第三方工具 Aspose 是一款付费产品,是我能够完成我需要做的事情的唯一方式。

    这是我的实现。这可以满足我的所有需求。

            local.msgStruct.attachments = [];
    
            // Create MapiMessage from the passed in .msg file
            MapiMessage = createObject("java", "com.aspose.email.MapiMessage");
            message = MapiMessage.fromFile(ARGUMENTS.msgFile);
    
            // Get attachments
            attachments = message.getAttachments();
            numberOfAttachments = attachments.size();
    
            // If attachments exist
            if(numberOfAttachments > 0) {
    
                // Loop over attachments
                for ( i = 0; i LT numberOfAttachments; i++) {
    
                    // Get current Attachment
                    currentAttachment = attachments.get_Item(i);
    
                    // Create struct of attachment info
                    local.attachmentInfo = {};
                    local.attachmentInfo.fileName = currentAttachment.getLongFileName();
                    local.attachmentInfo.fileExtension = currentAttachment.getExtension();
    
                    // If an attachmentDestination was specified
                    if(ARGUMENTS.attachmentDestination NEQ ''){
    
                    // Ignore inline image attchments (mostly email signature images)
                    if( NOT (left(local.attachmentInfo.fileName, 6) EQ 'image0' AND local.attachmentInfo.fileExtension EQ '.jpg') ){
    
                        // Get attachment object data (only defined for Outlook Messages, will return undefined object for other attachment types)
                        attachmentObjectData = currentAttachment.getObjectData();
    
                        // Check if attachment is an outlook message
                        if( isDefined('attachmentObjectData') AND attachmentObjectData.isOutlookMessage()){
                            isAttachmentOutlookMessage = 'YES';
                        } else {
                            isAttachmentOutlookMessage = 'NO';
                        }
    
                        ////////////////////////////
                        // ATTACHMENT IS AN EMAIL //
                        ////////////////////////////
                        if( isAttachmentOutlookMessage ){
    
                            // Get attachment as a MapiMessage
                            messageAttachment = currentAttachment.getObjectData().toMapiMessage();
    
                            // If an attachmentDestination was specified
                            if(ARGUMENTS.attachmentDestination NEQ ''){
    
                                // Set file path
                                local.attachmentInfo.filePath = ARGUMENTS.attachmentDestination;
    
                                // Set file path and file name
                                local.attachmentInfo.filePathAndFileName = ARGUMENTS.attachmentDestination & local.attachmentInfo.fileName;
    
                                // Save attachment to filesystem
                                messageAttachment.save(local.attachmentInfo.filePathAndFileName);
                            }
    
                        ////////////////////////////////
                        // ATTACHMENT IS NOT AN EMAIL //
                        ////////////////////////////////
                        } else {
    
                            // If an attachment destination was specified
                            if(ARGUMENTS.attachmentDestination NEQ ''){
    
                                // Set file path
                                local.attachmentInfo.filePath = ARGUMENTS.attachmentDestination;
    
                                // Set file path and file name
                                local.attachmentInfo.filePathAndFileName = ARGUMENTS.attachmentDestination & local.attachmentInfo.fileName;
    
                                // Save attachment to filesystem
                                currentAttachment.save(local.attachmentInfo.filePathAndFileName);
                            }
                        }
    
                        // Verify that the file was saved to the file system
                        local.attachmentInfo.savedToFileSystem = fileExists(ARGUMENTS.attachmentDestination & local.attachmentInfo.fileName);
    
                        // Add attachment info struct to array
                        arrayAppend(local.msgStruct.attachments,local.attachmentInfo);
    
                    } // End ignore inline image attachments
    
                } // End loop over attachments
    
            } // End if attachments exist
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多