Get Data from Outlook

  1. Microsoft.Office.Interop.Outlook.Application app = null;  
  2. Microsoft.Office.Interop.Outlook._NameSpace ns = null;  
  3. Microsoft.Office.Interop.Outlook.PostItem item = null;  
  4. Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;  
  5. Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;  
  6. try  
  7. {  
  8.     app = new Microsoft.Office.Interop.Outlook.Application();  
  9.     ns = app.GetNamespace("MAPI");  
  10.     ns.Logon(nullnullfalsefalse);  
  11.     inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);  
  12.     subFolder = inboxFolder.Folders["MySubFolderName"]; //folder.Folders[1]; also works    
  13.     Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);  
  14.     Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());  
  15.     for (int i = 1; i <= subFolder.Items.Count; i++)  
  16.     {  
  17.         item = (Microsoft.Office.Interop.Outlook.PostItem) subFolder.Items[i];  
  18.         Console.WriteLine("Item: {0}", i.ToString());  
  19.         Console.WriteLine("Subject: {0}", item.Subject);  
  20.         Console.WriteLine("Sent: {0} {1}"  
  21.             item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());  
  22.         Console.WriteLine("Categories: {0}", item.Categories);  
  23.         Console.WriteLine("Body: {0}", item.Body);  
  24.         Console.WriteLine("HTMLBody: {0}", item.HTMLBody);  
  25.     }  
  26. }  
  27. catch (System.Runtime.InteropServices.COMException ex)  
  28. {  
  29.     Console.WriteLine(ex.ToString());  
  30. }  
  31. finally  
  32. {  
  33.     ns = null;  
  34.     app = null;  
  35.     inboxFolder = null;  
  36. }