Reading emails & email headers from Outlook mail using C# – Part I

First, some news about myself, I left PiSigma Group, Islamabad and now working with DigiDent solutions Pvt. Ltd as Sr. Consultant. It’s a very good place to work in with lots of growth opportunities. At-least, hopefully, I will get paid on time here Smile

Now something about my latest work in this company. It was my first week and I was asked to do a task of reading all emails from a PST file (OUTLOOK 2007) and show all header information with subject, attachments and body of the message in a console application. Eventual goal, is to migrate all emails to GOOGLE. Interesting… hun?

Now first the scenario:

  1. Read all emails from PST a file. PST file can be the default PST or the user can specify his own PST file from a custom location.
  2. Read all headers of the email.
  3. Read message body & subject.
  4. Show attachments of the email.
  5. Show ‘TO’ and CC’d lists.


Reading emails and some headers is now very easy using VS 08 and referencing few Microsoft Office 2007 DLLs which are already present in "Add Reference" section of visual studio. So Microsoft helped us remove those worries and saved us from DLL hell and COM components. Please add reference of Microsoft.Office.Interop.Outlook version 12 in your application and use the API for further work.

using OutLook = Microsoft.Office.Interop.Outlook;

However, the problem arise when you need to read all header in just one go (based on RFC 822 email message standards), without specifying any specific headers. I hope, I am able to clearly mention the point of concern here. Moving further here is the piece of code which will help you read Outlook emails.

static OutLook.Application app = new OutLook.Application();

static OutLook.NameSpace outlookNs = app.GetNamespace("MAPI");

static OutLook.MAPIFolder rootFolder = outlookNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);


foreach (OutLook.MAPIFolder SubFolder in rootFolder.Folders)


if (SubFolder.DefaultItemType.ToString() == "olMailItem") //This will check for Mail folders only


if (SubFolder.Name != "Deleted Items")



GetSubFoldersAndItems(SubFolder, tvMailFolders.Nodes[i]);






This code will read all emails from a default store (default PST file). However, if you would like to read emails from a custom location you need to add that PST as store in your Outlook application and then iterate. Here’s is that piece of code as well. Furthermore, this code will show all Folders as "TreeNodes" and all emails and "Sub-nodes" by calling a re-cursive function (which is also given below).

outlookNs.AddStore(txtPSTFileLocation.Text); //txtPSTFileLocation is the textbox name.

OutLook.MAPIFolder rootFolder = null;


for (int j = 1; j <= outlookNs.Stores.Count; j++)


if (outlookNs.Stores[j].FilePath.ToString() == txtPSTFileLocation.Text)

rootFolder = outlookNs.Stores[j].GetRootFolder();



foreach (OutLook.MAPIFolder SubFolder in rootFolder.Folders)


if (SubFolder.DefaultItemType.ToString() == "olMailItem") //This will check for Mail folders only


if (SubFolder.Name != "Deleted Items")



GetSubFoldersAndItems(SubFolder, tvMailFolders.Nodes[i]); // See below this method’s definition





void GetSubFoldersAndItems(OutLook.MAPIFolder SubFolder, TreeNode aNode)


foreach (OutLook.MailItem item in SubFolder.Items)


if (SubFolder.Folders.Count > 0)


foreach (OutLook.MAPIFolder aFolder in SubFolder.Folders)



aNode.LastNode.ForeColor = Color.Red;

GetSubFoldersAndItems(aFolder, aNode.LastNode);



Now you can iterate all emails to show their headers, Subject and body.

foreach (OutLook.MailItem item in aSubFolder.Items)





lvEmailItems.Items[i].SubItems.Add(item.SentOn.ToLongDateString() + " " + item.SentOn.ToLongTimeString());



Now the problem was reading all headers in just one go and making an email message RFC 822 compliance which I will show in my next post how to do it… If anyone amongst you who would like to have this application/complete code, just please let me know, I can send it for you. Thanks and keep waiting for the next post.

Happy coding!



  1. badar

    Good Effort Dear …

  2. Hi Mr,

    my friend can you send all application to me


  3. Senthilkumar

    My pst got corrupted are u able to recover it ?

  4. Hi
    This is a excellent post and thanks for the time you have put in. Can you be so kind to send me the project.

    Much appreciated,

    best regards


  5. I dont have its source code with me anymore… Sorry guys… :S

  6. Ziko

    PST .NET

    Does not require Outlook and able to export all items as .msg and .eml files.

  7. akash

    can i get the source code … i needed it badly

  8. Hi dear friend ,

    Excellent code to read mails in outlook. I am in that situation to do this project. Can you send the project to me.

    • I do not have source code with me. I worked on this long way back but I tried to give lots of pointers here & some code as well. I hope it will help you. Thanks

  9. Toseef Abbasi.

    Great work plz send me the source code at

  10. Kristian Castaneda

    Can you please send me the source code?

    • Dear Kristian,

      Code is already in the post. I do not have full code with us. Thanks


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: