Click here to Skip to main content
12,395,871 members (61,675 online)
Click here to Skip to main content
Add your own
alternative version

Stats

93.1K views
5.3K downloads
48 bookmarked
Posted

Reading an Outlook MSG File in C#

, 12 Jun 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Going on with a great article where another author left in 2010

Introduction

First of all, this tip is an extended version for this great article --> http://www.codeproject.com/Articles/32899/Reading-an-Outlook-MSG-File-in-C. Actually, it is not an alternative to the original version, but just a new version.

The latest source can always be found on github. Just go to https://github.com/Sicos1977/msgreader.

Background

At my work, I needed something to read Outlook MSG and EML files . After searching on the internet, I found a nice article on CodeProject that did everything I needed. At least I thought it did.

After testing the code, I found out that some MSG files had the HTML part of the e-mail embedded into RTF. This is done by Microsoft to support legacy. A dozen years ago, it was normal to write E-mails in RTF format instead of HTML. I don't know why the HTML is sometimes embedded into the RTF, but it is.

Because that was a problem for me, I extended the original code with some extras:

  • The ability to read HTML that is embedded into the RTF body
  • The ability to read EML headers that are embedded into the MSG file when it is sent across the internet
  • Add an Outlook style header to the HTML or text file
  • The ability to save the MSG file as an HTML or text file to a folder
  • The ability to write all the attachments to a folder
  • The ability to use the code from a COM based language like VBA or VB6
  • Here and there, I fixed some issues that were found by other users

Bug Fixes and New Versions

When you do find a bug or have a future request, then just add a comment to this tip and I will see what I can do in my spare time.

Using the Code

Below, you see the most important class in this project. The class just does one thing, read the MSG file and save everything to an output folder. Through the IReader interface, the class is exposed to COM.

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using DocumentServices.Modules.Readers.MsgReader.Outlook;

namespace DocumentServices.Modules.Readers.MsgReader
{
    public interface IReader
    {
        /// <summary>
        /// Extract the input msg file to the given output folder
        /// </summary>
        /// <param name="inputFile" />The msg file
        /// <param name="outputFolder" />The folder where to extract the msg file
        /// <returns>String array containing the 
        /// message body and its (inline) attachments</returns>
        [DispId(1)]
        string[] ExtractToFolder(string inputFile, string outputFolder);

        /// <summary>
        /// This gives the 
        /// </summary>
        /// <returns>
        [DispId(2)]
        string GetErrorMessage();
    }

    [Guid("E9641DF0-18FC-11E2-BC95-1ACF6088709B")]
    [ComVisible(true)]
    public class Reader : IReader
    {
        ......
    } 

Example Program

Below, you see some screenshot from a test program that is included in the source files. This screenshot shows you an MSG file that is written to a temporary folder.

Single and double byte language support.

Task and follow up support.

Appointment support.

Contact(card) support.

History

  • 2014-07-06 Version 1.7

    • Added support for signed MSG files
    • Added support for EML (Mime 1.0 encoded) files
    • Fixed issue with double quotes not getting parsed correctly from HTML embedded into RTF
  • 2014-06-12 Version 1.6

    • Fixed bug in E-mail CC field that also went into the BCC field
    • Fixed bug in appointment mapping
    • Added AllAttendees, ToAttendees and CCAttendees properties to Appointment class
    • Added MSGMapper tool, with this tool properties from msg files can be mapped to extended file properties (Windows 7 or higher is needed for this)
    • Added Outlook properties to extended file properties mapping for:
      • E-mails
      • Appointments
      • Tasks
      • Contacts
  • 2014-04-29 Version 1.5
    • Added Outlook contact(card) support
    • Made properties late binding
  • 2014-04-21 Version 1.4
    • Full support for MAPI named properties
    • Added support for OLE attachments
    • Added Outlook Appointment support
    • Added Outlook Sticky notes support
    • Added support so that OLE images in RTF files get rendered correctly after conversion to HTML (tried to be as close as possible to how it looks in Outlook).
    • Extended E-mail support with RTF to HTML conversion. When there is no HTML body part and there is an RTF part, then this one gets converted to HTML.
    • Moved all language specific things to a separate class so that this component can be easily translated to other languages. Please send me the translations if you do this.
    • Fixed a lot of bugs and made speed improvements
  • 2014-03-30 Version 1.3
    • Completed implementing Outlook flag system on E-mail MSG object
    • Made all the MAPI functions private
    • Moved all language related things into a separate file so that it is easy to translate
    • Moved all MAPI constants to a separate file and added comment
    • Removed some unused classes
    • Cleaned up the code
    • Added option to convert attachment locations to hyperlinks
    • Fixed remove < and > when there is no e-mail address and only a displayname
    • Fixed issue with Sent On and Received on not being parsed as a DateTime values
    • Added support for categories in msg files (Outlook 2007 or later)
    • Fixed issue with e-mail address and displayname being swapped
    • Added RtfToHtmlConverter class (to convert RTF to HTML)
    • Added Message Type property so that we know what kind of MSG object we have
  • 2014-03-20 Version 1.2.1
    • Added support for double byte char sets like Chinese
  • 2014-03-18 Version 1.2
    • Fixed an issue with the Sent On (this was not set to the local timezone)
    • Added Received On, this is now added to the injected Outlook header
    • Added using statement to message object
    • Made some Native methods internal
    • Fixed some disposing issues, this was done more than once on some places
    • Refactored the code so that everything was correct according to the Microsoft code rules
  • 2014-03-06 Version 1.1
    • Added support for special characters like German umlauts, they are parsed out of the HTML text that is embedded inside the RTF
    • The RTFBody was loaded 4 times instead of once
    • A CC was always added even when there was no CC (the To was taken in that case)
    • Fixed some minor issues and cleaned up the code
  • 2014-01-16
    • First release

License

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

Share

About the Author

Kees van Spelde
Software Developer (Senior)
Netherlands Netherlands
Programming since I was a kid. Started on the Commodore 64 with BASIC. Since then I used programming languages like Turbo Pascal, Delphi, C++ and Visual Basic 6.

Now a days I do a lot of programming in C# with underlying databases like MS SQL

You may also be interested in...

Comments and Discussions

 
QuestionGood Work but what is Clear Signed MSG that is not supported ? Pin
Emanuele Bonin26-Feb-16 7:00
memberEmanuele Bonin26-Feb-16 7:00 
AnswerRe: Good Work but what is Clear Signed MSG that is not supported ? Pin
Kees van Spelde26-Feb-16 7:53
professionalKees van Spelde26-Feb-16 7:53 
GeneralRe: Good Work but what is Clear Signed MSG that is not supported ? Pin
Emanuele Bonin26-Feb-16 11:32
memberEmanuele Bonin26-Feb-16 11:32 
GeneralRe: Good Work but what is Clear Signed MSG that is not supported ? Pin
Emanuele Bonin28-Apr-16 2:59
memberEmanuele Bonin28-Apr-16 2:59 
PraiseCongrats Pin
Anderson Rissardi24-Feb-16 2:28
professionalAnderson Rissardi24-Feb-16 2:28 
GeneralRe: Congrats Pin
Kees van Spelde24-Feb-16 19:49
professionalKees van Spelde24-Feb-16 19:49 
Questionbackward compatible w/ .NET 3.5? Pin
Member 111259273-Jun-15 12:59
memberMember 111259273-Jun-15 12:59 
QuestionOpen *.msg with MsgViewer Pin
Member 1168409612-May-15 1:41
memberMember 1168409612-May-15 1:41 
AnswerRe: Open *.msg with MsgViewer Pin
Kees van Spelde12-May-15 6:53
professionalKees van Spelde12-May-15 6:53 
QuestionMargin in msg/eml files Pin
yushikuan7-May-15 8:25
memberyushikuan7-May-15 8:25 
AnswerRe: Margin in msg/eml files Pin
Kees van Spelde7-May-15 19:11
professionalKees van Spelde7-May-15 19:11 
SuggestionExtra Space problem Pin
malihe asadi30-Apr-15 14:02
membermalihe asadi30-Apr-15 14:02 
QuestionFonts are missing after extract MSG file to html Pin
yushikuan21-Apr-15 6:46
memberyushikuan21-Apr-15 6:46 
AnswerRe: Fonts are missing after extract MSG file to html Pin
Kees van Spelde21-Apr-15 19:10
professionalKees van Spelde21-Apr-15 19:10 
GeneralRe: Fonts are missing after extract MSG file to html Pin
yushikuan22-Apr-15 8:31
memberyushikuan22-Apr-15 8:31 
GeneralRe: Fonts are missing after extract MSG file to html Pin
Kees van Spelde29-Apr-15 6:37
professionalKees van Spelde29-Apr-15 6:37 
GeneralRe: Fonts are missing after extract MSG file to html Pin
yushikuan30-Apr-15 11:37
memberyushikuan30-Apr-15 11:37 
QuestionGreat Work..But I have one issue.. Pin
Ritesh1219-Mar-15 9:18
memberRitesh1219-Mar-15 9:18 
AnswerRe: Great Work..But I have one issue.. Pin
Kees van Spelde9-Mar-15 9:37
professionalKees van Spelde9-Mar-15 9:37 
GeneralRe: Great Work..But I have one issue.. Pin
Ritesh12110-Mar-15 4:53
memberRitesh12110-Mar-15 4:53 
GeneralRe: Great Work..But I have one issue.. Pin
Kees van Spelde10-Mar-15 8:25
professionalKees van Spelde10-Mar-15 8:25 
GeneralRe: Great Work..But I have one issue.. Pin
Kees van Spelde11-Mar-15 8:34
professionalKees van Spelde11-Mar-15 8:34 
GeneralRe: Great Work..But I have one issue.. Pin
Member 118654116-Sep-15 21:47
memberMember 118654116-Sep-15 21:47 
AnswerRe: Great Work..But I have one issue.. Pin
Kees van Spelde8-Sep-15 6:06
professionalKees van Spelde8-Sep-15 6:06 
QuestionRetrieving specific property : PR_ENTRYID Pin
mourad_K12-Feb-15 6:11
membermourad_K12-Feb-15 6:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 12 Jun 2014
Article Copyright 2014 by Kees van Spelde
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid