Highlighted

Extracting email attachments from .msg files in ColdFusion

Community Beginner ,
Nov 28, 2017

Copy link to clipboard

Copied

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>

Correct answer by DanJCard | Community Beginner

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.

Views

1.3K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Extracting email attachments from .msg files in ColdFusion

Community Beginner ,
Nov 28, 2017

Copy link to clipboard

Copied

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>

Correct answer by DanJCard | Community Beginner

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.

Views

1.3K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 28, 2017 0
Community Beginner ,
Nov 29, 2017

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 29, 2017 2
Community Beginner ,
Nov 29, 2017

Copy link to clipboard

Copied

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!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 29, 2017 1
Community Beginner ,
Dec 07, 2017

Copy link to clipboard

Copied

Hi again,

Thanks again for your response, it got me past my last problem perfectly! I have one more final issue (hopefully) that I'm getting stuck on. Picking up from where we left off above, I'm able to write each email attachment to the file system, unless the attachment is another email (isEmbeddedMessage() returns true). When I drill into the attachments array returned by getAttachmentFiles() I see that the attachment is another MAPIMessage object. I see all the familiar methods to get the details about this message just as I can about the original message to which this message is an attachment.

 

When I call the getEmbeddedAttachmentObject() method on the message attachment, it returns "undefined". In fact I see no way of getting the MAPIMessage object in binary form so I can write it to the file system like I did the non-message attachments.  The MAPIMessage object does have a write() method, but upon calling it I get an error stating "Note - writing is not yet supported for this file format, sorry." This is backed up by the documentation on poi.apache.org as well.

 

To summarize, I can write each email message attachement to the file system without a problem, unless the attachment is another email message.

 

Thanks again for your time and your help. I really appreciate it!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 07, 2017 0