使用 ColdFusion 从 Outlook .msg 文件中提取附件

Extracting attachments from Outlook .msg files using ColdFusion

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

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

Note - writing is not yet supported for this file format, sorry.

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>

经过大量研究,我找到了解决问题的办法。由于尚未实现 write() 方法,我无法使用 ColdFusion 随附的 org.apache.poi.hsmf.MAPIMessage java 对象保存嵌入式消息文件。相反,我使用了一个名为 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