I am building a system where intranet users are allowed to drag and drop files into a div on our ColdFusion 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. This is possible using the org.apache.poi.hsmf.MAPIMessage Java object.
With the following code I am able to see each attachment object listed out. I can then get their filenames and extensions and save each one to the local file system.
However, this does not work if the attachment is another .msg file. When I call getEmbeddedAttachmentObject() on an attached .msg file, it returns an object which contains only "undefined". Non .msg files return a binary object which I can then pass into the FileWrite() ColdFusion function. Further examination of the MAPIMessage object shows that it has 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 http://poi.apache.org as well.
To summarize, I can write each email message attachment to the file system without a problem, unless the attachment is another email message. Am I out of luck or is there another way to accomplish 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'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>