Search code examples
coldfusioncoldfusion-9

ColdFusion: Extract information from a .msg file


I would like to make an application where the user drags in a .msg file into my web app. Then ColdFusion would extract the following fields: name, sender email, subject, etc. and pre-populate it in a form ready to be submitted. I have googled "read .msg ColdFusion" but can't seem to find any information. When I do a FileRead() I see only gibberish ÐÏࡱá > þÿ because it's encrypted. Is this even possible? I hope someone can point me in the right direction. I am also open to trying a different approach.


Solution

  • As @imthepitts mentioned, the file is not encrypted, it is just in binary. However, it is not enough to just load the bytes with fileReadBinary(). You need a tool that understands the format of .msg files and can parse its contents.

    If you do a quick search, there are a bunch of tools capable of parsing .msg files (most are java or .net). One such tool is POI's HSMF (Horrible Stupid Mail Format). It is already built into CF. So you may want to start there.

    Here is a quick and dirty example translated from the HSMF examples:

    <cfscript>
        pathToFile = "c:/path/to/someMessage.msg";
        MAPIMessage = createObject("java", "org.apache.poi.hsmf.MAPIMessage");
        message = MAPIMessage.init(pathToFile);
    
    
        try {
            WriteOutput("From: "& message.getDisplayFrom() &"<hr>");
            WriteOutput("To: "& message.getDisplayTo() &"<hr>");
            WriteOutput("CC: "& message.getDisplayCC() &"<hr>");
            WriteOutput("BCC: "& message.getDisplayBCC() &"<hr>");
            WriteOutput("Subject: "& message.getSubject() &"<hr>");
            WriteOutput("Body: "& message.getTextBody() &"<hr>");
        } catch (org.apache.poi.hsmf.exceptions.ChunkNotFoundException e) {
            WriteDump(e);
        }
    </cfscript>