Friday, December 26, 2008

How to parse mails in Microsoft Outlook using C#?

We often get hundreds of mails in our Outlook Inbox and want to do some processing with these mails. This scenario arises mainly with the customer support people who wanted to act depending on the mail subject line. Here is a simple code to parse the Microsoft Outlook Inbox:


static void ReadMail()
{
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.MailItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;

try
{
app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI");
ns.Logon(null, null, false, false);

inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
// subFolder = inboxFolder.Folders["Inbox"]; //folder.Folders[1]; also works
Console.WriteLine("Folder Name: {0}, EntryId: {1}", inboxFolder.Name, inboxFolder.EntryID);
Console.WriteLine("Num Items: {0}", inboxFolder.Items.Count.ToString());

System.IO.StreamWriter strm = new System.IO.StreamWriter(@"d:\Test\Inbox.txt");
for (int counter = 1; counter <= inboxFolder.Items.Count; counter++)
{
item = (Microsoft.Office.Interop.Outlook.MailItem)inboxFolder.Items[counter];
Console.WriteLine("Item: {0}", counter.ToString());
Console.WriteLine("Subject: {0}", item.Subject);
Console.WriteLine("Sent: {0} {1}", item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());
Console.WriteLine("Sendername: {0}", item.SenderName);
strm.WriteLine(counter.ToString() + "," + item.Subject + "," + item.SentOn.ToShortDateString() + "," + item.SenderName);
}
strm.Close();
}
catch (System.Runtime.InteropServices.COMException comException)
{
Console.WriteLine(comException.ToString());
}
finally
{
ns = null;
app = null;
inboxFolder = null;
}

}
Hope this helps you :)

5 comments:

Anonymous said...

Thank you very much for an excellent piece of code.

crown said...

Hi,

The example is good. But it triggers the outlook security prompt. Am I missing something ?

Thanks

Alex said...

In this situation I advise to use this software-ost to pst convertor,because tool has many facilities and it is free as far as I can see,also software helped me many times and program is reliable,it recover data from *.ost format and convert it to files with *.pst extension,restore your personal information, contacts, messages, tasks and calendars may be lost,extract your data from *.ost file, it is stored just to speed up your connection with Microsoft Exchange Server,permit to convert *.ost file to *.pst extension, that can be easily read by any email client, compatible with Microsoft Outlook. Owing to instability of *.ost files, they are often corrupted and may contain viruses, cause email is one of the most popular ways to distribute malware,allows to read even corrupted files and you do not need to put any modifications to recovery process,will never modify source files, so, if you are not satisfied with this program, you can always try to return back to the beginning of process.

Anonymous said...

Crown... I "solved" that problem by modifying my settings in the Trust Center. Unfortunately, it's not a good solution. I had to tell Outlook to not warn me about suspicious activity. That makes me a little paranoid, but it solved the programmatic issue.

Alex said...

Today I worked with my Inbox folder in MS Outlook and renamed it.But after all files in it were damaged.And my good friend-programmer recommended me-outlook contacts recovery ost file.To my great surprise tool helped me in two minutes.It has got free status as is known.

Post a Comment