Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / C#
Article

MsgReader - DLL

Rate me:
Please Sign up or sign in to vote.
3.42/5 (14 votes)
16 Jul 2007CPOL2 min read 83.3K   4.8K   28   11
A library which can be used to read Outlook msg-files without Outlook

Introduction

The MsgReader.dll is able to read a Outlook msg-file without having Microsoft Outlook installed. The library is able to read, e.g. Subject, message body (also compressed RTF), saving attachments (no msg attachments), ...

It can be used by everybody for non-commercial projects.

Background

To read the OLE-Storage, we used the Edanmo.OleStorage.dll. A lot of other code was derived, ported, ... from code snips we found on the internet.

Using the Code

The library was developed with C# and .NET 1.1. Just import it and build your application. It might contain some bugs because this is the first working version we "released". Tests was done with Microsoft Outlook 2003.
For more details, check out MsgReader.chm which contains the whole documentation.

The following code shows a small example of how to use the library:

  1. Open the selected Outlook msg-file:

    C#
    private void btnBrowse_Click(object sender, System.EventArgs e)
    {
      this.opnFile.ShowDialog();
      this.txtBoxFile.Text = this.opnFile.FileName;
    }
  2. Read the To, From, Subject and mail body (plain text & RTF-formatted text) property from msg-file and display them:

    C#
    private void btnShow_Click(object sender, System.EventArgs e)
    {
      if (this.txtBoxFile.Text != "")
      {
        string[] AttachList;
        string Content = "";
        string path = this.txtBoxFile.Text;
        try
        {
          // First of all you have to create new object
          // the constructor has to take as a argument
          // path to file which we'd like to open
          Msg storage = new Msg(path);
                        
          this.txtBoxTo.Text = storage.GetToProperty();
          this.txtBoxFrom.Text = storage.GetFromProperty();
          this.txtBoxSub.Text = storage.GetSubjectProperty();
          this.txtBoxBody.Text = storage.GetBodyText();
                        
          storage.GetAttachmentList(out AttachList);
                        
          Content = storage.GetBodyRtfText();
          this.RTFBox.TextRTF = addAttachmentsToRTF(Content, AttachList);
          storage.Close();
        }
        catch
        {
          // cannot open file
        }
      }
    }
  3. This function adds the Windows file type icons of the attachments and the attachments name to the RTF-Body. Only in case the attachments were originally "attached" to an RTF or HTML body, the icon will be displayed in the RTF-body because in case of plain-text mails, no attachments rendering position is defined!

    C#
    private string addAttachmentsToRTF(string RTFBody, string[] attchList)
    {
      int lastIndex = 0;
      WMETA8 wmeta8;
      String dummy;
    
      if (RTFBody.Length > 13) 
      {
        for (int i=0; i<attchList.Length; i++)
        {
          lastIndex = RTFBody.IndexOf("\\objattph\\'20", lastIndex);
          if (lastIndex > 0) 
          {
            RTFBody = RTFBody.Remove(lastIndex, 13);
            wmeta8 = new WMETA8(this.RTFBox.CreateGraphics());
            dummy = attchList[i];
            if (dummy.Length > 15) 
            {
              dummy = "<" + dummy.Substring(0,13) + "..>";
            }
            else 
            {
              dummy = " <" + dummy + ">";
            }
            RTFBody = RTFBody.Insert(lastIndex, 
    		(wmeta8.getRTFIcon(attchList[i]) + "{\\fs16 " + dummy +"}"));
          }
          else break;
        }
      }
                
      return RTFBody;
    }

The complete Visual Studio project can be also downloaded (TestofMsgReaderDll.zip):

Screenshot - Sample_picture.jpg

Points of Interest

More details, news, and updates regarding the MsgViewer-Application and the DLL can be found here.

History

  • 12.7.2007 - Picture of the sample application added
  • 17.7.2007 - Sorry for the delay - we had some trouble with the upload. Today I added the new DLL-Project (done with Visual Studio .NET 2003) and the full library documentation

License

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


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSubmit corrections? Pin
Berend Engelbrecht23-Nov-09 3:30
Berend Engelbrecht23-Nov-09 3:30 
AnswerRe: Submit corrections? Pin
RussGreen10-Apr-10 0:58
RussGreen10-Apr-10 0:58 
GeneralSaveAttachToFile Pin
remember f6-Jan-09 23:28
remember f6-Jan-09 23:28 
GeneralRe: SaveAttachToFile Pin
articx30-Jan-17 5:42
articx30-Jan-17 5:42 
GeneralCreating a Strong Name for MsgReader.dll [modified] Pin
Vipul Mehta15-Dec-08 20:49
Vipul Mehta15-Dec-08 20:49 
GeneralRe: Creating a Strong Name for MsgReader.dll Pin
ltcartman18-Dec-08 9:29
ltcartman18-Dec-08 9:29 
QuestionJust a little bug ? Pin
gantoissylvain@hotmail.com4-Jun-08 6:46
gantoissylvain@hotmail.com4-Jun-08 6:46 
QuestionWhere to get latest release? Pin
Bilal Haidar5-May-08 2:15
Bilal Haidar5-May-08 2:15 
GeneralWhere is the source code Pin
Dewey12-Jul-07 21:24
Dewey12-Jul-07 21:24 
GeneralRe: Where is the source code Pin
PSI200616-Jul-07 21:25
PSI200616-Jul-07 21:25 
GeneralScreenshot Pin
Michael Sync11-Jul-07 22:34
Michael Sync11-Jul-07 22:34 

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.