Click here to Skip to main content
Click here to Skip to main content
Go to top

Reading an Outlook MSG File in C#

, 8 Jul 2010
Rate this:
Please Sign up or sign in to vote.
How to read an Outlook msg file in C# without the Outlook object model
Demo application form

Introduction

This article is going to focus on how to dissect a msg file generated by Outlook. It covers how to read the basic properties of the mail message, attachments and any msg attachments (these need to be handled differently).

Using the Code

The code is pretty simple to use. You construct a new instance of the OutlookStorage.Message class, sending it the path to a msg file or a Stream containing an IStorage. The Stream constructor is provided so that it is easy to integrate with the Outlook drag and drop code in another of my articles and this is shown in the demo application.

private static void main()
{
    //create new Outlook message from file
    OutlookStorage.Message outlookMsg = new OutlookStorage.Message(@"C:\test.msg");
    DisplayMessage(outlookMsg);
}

private static void DisplayMessage(OutlookStorage.Message outlookMsg)
{    
    Console.WriteLine("Subject: {0}", outlookMsg.Subject);
    Console.WriteLine("Body: {0}", outlookMsg.BodyText);
    
    Console.WriteLine("{0} Recipients", outlookMsg.Recipients.Count);    
    foreach (OutlookStorage.Recipient recip in outlookMsg.Recipients)
    {
        Console.WriteLine(" {0}:{1}", recip.Type, recip.Email);
    }
    
    Console.WriteLine("{0} Attachments", outlookMsg.Attachments.Count);
    foreach (OutlookStorage.Attachment attach in outlookMsg.Attachments)
    {
        Console.WriteLine(" {0}, {1}b", attach.Filename, attach.Data.Length);
    }

    Console.WriteLine("{0} Messages", outlookMsg.Messages.Count);
    foreach (OutlookStorage.Message subMessage in outlookMsg.Messages)
    {
        DisplayMessage(subMessage);
    }
}

Save a Msg and All Attachments to the File System

This is an example on how to save a message and all associated attachments to the application path.

private static void main()
{
    //create new Outlook message from file
    OutlookStorage.Message outlookMsg = new OutlookStorage.Message(@"C:\test.msg");
}

private static void SaveMessage(OutlookStorage.Message outlookMsg)
{    
    outlookMsg.Save(outlookMsg.Subject.Replace(":", ""));    

    foreach (OutlookStorage.Attachment attach in outlookMsg.Attachments)
    {
        byte[] attachBytes = attach.Data;
        FileStream attachStream = File.Create(attach.Filename);
        attachStream.Write(attachBytes, 0, attachBytes.Length);
        attachStream.Close();
    }

    foreach (OutlookStorage.Message subMessage in outlookMsg.Messages)
    {
        SaveMessage(subMessage);
    }
}

Understanding the Code

To read the msg file produced by Outlook, there are two concepts to understand. The first is that an msg file is logically a MAPI object with MAPI properties and the second is that phyiscally the MAPI object and its properties are stored in an IStorage. Microsoft has kindly provided a specification on how the MAPI properties are mapped to the IStorage, so at this point I will defer to that and just go over the catches that popped up when figuring out how to save a sub message out of its parent.

Saving a Sub Message

Saving a sub message out of the parent message has a few catches. The property stream header needs to be padded and the name to id mapping storage needs to be copied to the sub message storage.

Fixing the Property Stream

MAPI property values can be stored in a sub storage, a sub stream or in the case of fixed size values (like an integer) a special sub stream called the property stream. The property stream consists of a variable length header and then an array of 16 byte pairs with a property identifier in the first 8 bytes and the property value in the second 8.

It is the variable length header that you should take note of. It is 8 bytes for an attachment or recipient storage, 32 bytes for a top level msg and 24 bytes for a sub msg. This means that if you want to extract a sub message and save it without its parent you need to pad the end of the header with 8 null bytes.

The Name to Id Mapping

The other catch for saving a sub message is the name to id mapping storage which only exists on the top level msg, but contains the mappings for the entire tree. So when saving a sub message, this storage needs to be copied to it before saving for it to be valid.

Conclusion

Everything is encapsulated in the OutlookStorage.cs file as I don't like to release stuff with dependencies and prefer to just be able to drop a CS into my projects to get a particular piece of functionality. There is a region in there under a separate licence for the code to decompress the compressed RTF, but it is clearly marked.

History

  • 8th July, 2010
    • Fixed memory leak
    • Fixed the "COM object that has been separated from its underlying RCW cannot be used" exception
    • Added better determination of attachment file name
  • 28th January, 2009: Original article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

David Ewen
Founder Guava Development
Australia Australia
I am the Founder of Guava Development a Software Services Company located in Perth, Western Australia dedicated to improving productivity and reducing costs through the targeted and innovative application of software assisted workflows and packages.
 
I have been working in the industry for 10 years. My day job usually involves programming with C# but I have been known to mess around with just about everything.
Follow on   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 PinmemberAndy Roberts31-May-14 9:05 
QuestionWorks great until I try to read a recipient that has an underscore in their e-mail address PinmemberMember 1068027621-Mar-14 7:36 
AnswerRe: Works great until I try to read a recipient that has an underscore in their e-mail address PinprofessionalKees van Spelde22-Mar-14 7:37 
GeneralRe: Works great until I try to read a recipient that has an underscore in their e-mail address PinmemberMember 1068027624-Mar-14 3:50 
GeneralRe: Works great until I try to read a recipient that has an underscore in their e-mail address PinprofessionalKees van Spelde24-Mar-14 7:52 
GeneralRe: Works great until I try to read a recipient that has an underscore in their e-mail address PinmemberMember 1068027625-Mar-14 8:11 
GeneralRe: Works great until I try to read a recipient that has an underscore in their e-mail address PinmemberMember 1068027626-Mar-14 10:44 
GeneralRe: Works great until I try to read a recipient that has an underscore in their e-mail address PinprofessionalKees van Spelde26-Mar-14 20:50 
GeneralRe: Works great until I try to read a recipient that has an underscore in their e-mail address PinprofessionalKees van Spelde27-Mar-14 8:45 
QuestionThe development and maintenance of this project goes on somewhere else PinprofessionalKees van Spelde17-Jan-14 7:28 
QuestionHow to get Start time/End time?? PinmemberVORUN17-Dec-13 10:58 
AnswerRe: How to get Start time/End time?? [modified] Pinmembersicos17-Dec-13 22:34 
GeneralRe: How to get Start time/End time?? PinmemberVORUN15-Jan-14 13:31 
GeneralRe: How to get Start time/End time?? PinmemberDavid Ewen15-Jan-14 13:53 
GeneralRe: How to get Start time/End time?? PinprofessionalKees van Spelde22-Mar-14 8:04 
GeneralRe: How to get Start time/End time?? PinprofessionalKees van Spelde15-Jan-14 19:16 
GeneralRe: How to get Start time/End time?? PinprofessionalKees van Spelde21-Apr-14 1:37 
QuestionMSG .NET Pinmemberparis paris13-Dec-13 19:50 
AnswerRe: MSG .NET Pinmembersicos16-Dec-13 0:22 
GeneralWorks great! PinmemberJohn McCullough27-Nov-13 4:07 
GeneralRe: Works great! [modified] Pinmembersicos27-Nov-13 7:34 
GeneralRe: Works great! Pinmemberandycwk15-Jan-14 11:03 
GeneralRe: Works great! PinprofessionalKees van Spelde15-Jan-14 19:12 
QuestionHow can I get HTML Body Text Pinmemberxieyijun14-Nov-13 20:58 
AnswerRe: How can I get HTML Body Text PinprofessionalKees van Spelde29-Mar-14 9:33 
Questionoutlook message as attachment PinmemberJaleelali16-Oct-13 11:20 
AnswerRe: outlook message as attachment PinprofessionalKees van Spelde21-Apr-14 1:38 
GeneralGreat, useful, smart solution PinmemberViktornLiu23-Jul-13 3:41 
QuestionMessage Automatically Removed PinmemberMax Xbox6-Jul-13 2:29 
QuestionHow about ReceiveTime and SentOn? Pinmemberxidongzhang22-Jun-13 11:29 
AnswerRe: How about ReceiveTime and SentOn? Pinmemberd0njudd8-Aug-13 10:24 
GeneralRe: How about ReceiveTime and SentOn? Pinmemberxidongzhang8-Aug-13 16:41 
GeneralRe: How about ReceiveTime and SentOn? Pinmemberyrk222922-Sep-13 16:15 
GeneralRe: How about ReceiveTime and SentOn? Pinmemberxidongzhang13-Nov-13 4:51 
GeneralRe: How about ReceiveTime and SentOn? PinmemberJamie McAllister19-Dec-13 4:24 
QuestionEncoding Problem Pinmemberarvelius1-May-13 22:44 
AnswerRe: Encoding Problem PinprofessionalKees van Spelde29-Mar-14 9:33 
QuestionAttached vs Embedded PinmemberSharpi90098-Feb-13 5:33 
AnswerRe: Attached vs Embedded Pinmemberfryezz27-Mar-13 6:44 
QuestionHow to Open the signed ,msg file PinmemberV G S Naidu A20-Jan-13 19:25 
QuestionHow to display images into RichTextBox from Outlook message file(.MSG)? PinmemberRameshMorasa30-Aug-12 21:41 
GeneralMy vote of 5 PinmemberJustDeveloper31-Jul-12 5:11 
QuestionPls add a new Revision with Sender PinmemberDePaule4-Jul-12 23:08 
QuestionHow to i make this reverse? Pinmemberphantomlakshan17-May-12 4:56 
QuestionReply-to Pinmemberjanipiombo8-May-12 8:57 
AnswerRe: Reply-to Pinmemberziotullio14-Sep-12 21:41 
Questionopen attachment on double click PinmemberSaurabhSavaliya26-Apr-12 23:37 
QuestionEncoding problem when part of a WCF method Pinmemberzaf23-Apr-12 1:02 
QuestionDisposing fails on wrong files Pinmemberlacroix22-Mar-12 9:25 
AnswerRe: Disposing fails on wrong files PinprofessionalKees van Spelde29-Mar-14 9:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 8 Jul 2010
Article Copyright 2009 by David Ewen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid