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

问题描述 投票:5回答:1

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

但是,如果附件是另一个.msg文件,则不起作用。当我在附加的getEmbeddedAttachmentObject()文件上调用.msg时,它返回一个仅包含“undefined”的对象。非.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个回答
0
投票

经过大量研究后,我找到了解决问题的方法。由于尚未实现的org.apache.poi.hsmf.MAPIMessage方法,我无法使用由ColdFusion附带的write() 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
© www.soinside.com 2019 - 2024. All rights reserved.