• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
0

Extracting email attachments from .msg files in ColdFusion

Community Beginner ,
Nov 28, 2017 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>

Views

2.1K

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
community guidelines

correct answers 1 Correct answer

Community Beginner , Nov 29, 2017 Nov 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=attachmen

...

Votes

Translate

Translate
Community Beginner ,
Nov 29, 2017 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.

Votes

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
community guidelines
Community Beginner ,
Nov 29, 2017 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!

Votes

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
community guidelines
Community Beginner ,
Dec 07, 2017 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!

Votes

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
community guidelines
Community Expert ,
Mar 29, 2024 Mar 29, 2024

Copy link to clipboard

Copied

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

 

Dave Watts, Eidolon LLC 

Votes

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
community guidelines
Enthusiast ,
Mar 30, 2024 Mar 30, 2024

Copy link to clipboard

Copied

LATEST

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.

Votes

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
community guidelines
Resources
Documentation