Extract attachments from Outlook.msg files using ColdFusion

I am creating a system where intranet users are allowed to drag and drop files into divour ColdFusion website, which after some verification will automatically upload them to the file server. One of my requirements: when the downloaded file is a file .msg(Outlook Email), extract any files that are attachments to this email and download them separately. This is possible using a org.apache.poi.hsmf.MAPIMessageJava object . Following the code below, I can see how each of the listed objects is attached. Then I can get their names and extensions and save them to the local file system.

However, this does not work if the attachment is a different file .msg. When I call getEmbeddedAttachmentObject()in the attached file .msg, it returns an object that contains only "undefined". Files .msgreturn a binary object that I can then pass to the FileWrite()ColdFusion function . Further examination of the object MAPIMessageshows that it has a method write(), but after calling it, I get an error message:

Note. Recording on this file is not yet supported.

This is confirmed by the documentation at http://poi.apache.org .

To summarize, I can write each attachment of an electronic message to the file system without problems if the attachment is not another electronic message. Am I lucky or is there another way to do this?

<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 binary data
      local.data=attachments[i].getEmbeddedAttachmentObject();

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

      // Get attachment 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>
+4
1

. msg org.apache.poi.hsmf.MAPIMessage java-, ColdFusion - write(). Aspose.Email 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
0

Source: https://habr.com/ru/post/1690538/


All Articles