Skip to main content
CraigVerrastro
Participant
November 28, 2017
Answered

Extracting email attachments from .msg files in ColdFusion

  • November 28, 2017
  • 3 replies
  • 2831 views

Hello,

I am building a system where intranet users are allowed to drag and drop files into a div on our site, which after some validation will then automatically upload them to a file server. One of my requirements is: when the file which was uploaded is a .msg file (Outlook Email), extract any files which are attachments to that email and upload them individually.  I cannot find an existing solution to accomplish this online.

The closest thing I found is this https://stackoverflow.com/questions/16473421/coldfusion-extract-information-from-a-msg-file  which is fantastic, as it includes everything I need except how to extract the attachments. I believe it is possible with the correct usage of the org.apache.poi.hsmf.MAPIMessage object. 

With the following code I am able to see each attachment object, and it's filename and extension. I just can't figure out how to save each object to the file system.  Any help is GREATLY appreciated!!

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

 local.msgStruct = {};
 
 local.msgStruct.from = message.getDisplayFrom();
 local.msgStruct.to = message.getDisplayTo();
 
 attachments = message.getAttachmentFiles(); 
 
 if(arrayLen(attachments) > 0) {
   
  for (i=1; i LTE arrayLen(attachments); i++) {
   writeDump( attachments );
   
   attachmentFileName = attachments.attachLongFileName.toString();
   attachmentExtension = attachments.attachExtension.toString();
   
   writeDump( attachmentFileName );
   writeDump( attachmentExtension );
  }

}
</cfscript>

This topic has been closed for replies.
Correct answer DanJCard

You're 95% there.

When you dump out message.getAttachmentFiles() you'll seean array of org.apache.poi.hsmf.datatypes.AttachmentChunks. Each of these has a method called getEmbeddedAttachmentObject(). This is the entire binary of the file. You can then write that to a file using FileWrite.

Here's your array loop:

attachments = message.getAttachmentFiles();

if(arrayLen(attachments) > 0) {

   for (i=1; i LTE arrayLen(attachments); i++) {

          writeDump( attachments );

          local.data=attachments.getEmbeddedAttachmentObject();

          writeDump( local.data );

          attachmentFileName = attachments.attachLongFileName.toString();

          attachmentExtension = attachments.attachExtension.toString();

          FileWrite("#expandPath('/')##attachments.attachLongFileName.toString()#", local.data);

          writeDump( attachmentFileName );

          writeDump( attachmentExtension );

     }

}

The difficulty you're going to run into is actually not with the attachments but with the message itself. In an MSG file, the message is stored in either HTML, RTF, or text. Notice I said, "either" not "AND". If it's in HTML you're golden and can just display it. If it's stored in text you lose your formatting but have the words. If it's in RTF, you need to find (or build) and RTF parser and I wasn't able to find a good one last year when I was looking. Incidently, would love anyone's recommendations.

Let me know how it goes.

3 replies

James Moberg
Inspiring
March 30, 2024

Hey @Alice35967071qacc How is this supposed to work with ColdFusion?

" Softaken MSG Attachment Extractor Tool" appears to be a human-driven, GUI-only solution. A developed would have to hire someone around the clock to transfer MSG/EML files to a local directory and then use this program to click the buttons and generate the desired exports. (Seems like a lot of work.) There doesn't seem to be any directory/file watcher services or CLI functionality. Please advise.

Community Expert
March 29, 2024

Does this have an API that can be used from ColdFusion?

 

Dave Watts, Eidolon LLC 

Dave Watts, Eidolon LLC
DanJCardCorrect answer
Participant
November 29, 2017

You're 95% there.

When you dump out message.getAttachmentFiles() you'll seean array of org.apache.poi.hsmf.datatypes.AttachmentChunks. Each of these has a method called getEmbeddedAttachmentObject(). This is the entire binary of the file. You can then write that to a file using FileWrite.

Here's your array loop:

attachments = message.getAttachmentFiles();

if(arrayLen(attachments) > 0) {

   for (i=1; i LTE arrayLen(attachments); i++) {

          writeDump( attachments );

          local.data=attachments.getEmbeddedAttachmentObject();

          writeDump( local.data );

          attachmentFileName = attachments.attachLongFileName.toString();

          attachmentExtension = attachments.attachExtension.toString();

          FileWrite("#expandPath('/')##attachments.attachLongFileName.toString()#", local.data);

          writeDump( attachmentFileName );

          writeDump( attachmentExtension );

     }

}

The difficulty you're going to run into is actually not with the attachments but with the message itself. In an MSG file, the message is stored in either HTML, RTF, or text. Notice I said, "either" not "AND". If it's in HTML you're golden and can just display it. If it's stored in text you lose your formatting but have the words. If it's in RTF, you need to find (or build) and RTF parser and I wasn't able to find a good one last year when I was looking. Incidently, would love anyone's recommendations.

Let me know how it goes.

CraigVerrastro
Participant
November 29, 2017

Thank you so much!!  This worked perfectly and is exactly what I needed! 

Luckily I don't need the contents of the email, only the attachments within it, but thanks very much for the tip!