Advertisement
Guest User

paul

a guest
Jan 26th, 2010
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Office.Interop.Outlook;
  6. using Microsoft.Office.Interop;
  7. using System.Runtime.InteropServices;
  8.  
  9. namespace HandleMailingResponses
  10. {
  11.     class OutlookFolderTableScraper
  12.     {
  13.         public List<OutlookItem> GetItemsFromFolder(string folderName)
  14.         {
  15.             List<OutlookItem> returnList = new List<OutlookItem>();
  16.  
  17.             Application outlookHandle = new Application();
  18.             NameSpace outlookNamespace = outlookHandle.GetNamespace("MAPI");
  19.             Folders rootOutlookFolders = outlookNamespace.Folders;
  20.  
  21.             outlookNamespace.Logon(null, null, null, true);
  22.  
  23.             Folder requestedRoot = enumerateFolders(rootOutlookFolders, folderName);
  24.             Folders theseFolders = requestedRoot.Folders;
  25.             Folder thisInbox = enumerateFolders(theseFolders, "Inbox");
  26.  
  27.             Marshal.ReleaseComObject(requestedRoot);
  28.             requestedRoot = null;
  29.             Marshal.ReleaseComObject(rootOutlookFolders);
  30.             rootOutlookFolders = null;
  31.            
  32.             string storeID = thisInbox.StoreID;
  33.  
  34.             Table thisTable = thisInbox.GetTable("",OlTableContents.olUserItems);
  35.  
  36.             //By default each item has the columns EntryID, Subject, CreationTime, LastModificationTime and MessageClass
  37.             //we can add any of the other properties the MailItem or ReportItem object would have....
  38.             Columns theseColumns = thisTable.Columns;
  39.             theseColumns.Add("SenderEmailAddress");
  40.  
  41.             Marshal.ReleaseComObject(thisInbox);
  42.             thisInbox = null;
  43.  
  44.             outlookNamespace.Logoff();
  45.             Marshal.ReleaseComObject(outlookNamespace);
  46.             outlookNamespace = null;
  47.             outlookHandle.Quit();
  48.             Marshal.ReleaseComObject(outlookHandle);
  49.             outlookHandle = null;
  50.  
  51.             int count = 0;
  52.             while (!thisTable.EndOfTable)
  53.             {
  54.                 Row thisRow = thisTable.GetNextRow();
  55.                 object[] theseValues = (object[]) thisRow.GetValues();
  56.                 Console.WriteLine("processed {0}",count++);
  57.  
  58.                 //get the body from this item
  59.                 string messageClass = (string)theseValues[4];
  60.                 string entryID = (string)theseValues[0];
  61.                 string body = getItemBody(entryID,storeID, messageClass);
  62.                
  63.                 returnList.Add(new OutlookItem((string)theseValues[5], (string)theseValues[1], body, messageClass, entryID));
  64.             }
  65.  
  66.  
  67.  
  68.             return returnList;
  69.         }
  70.  
  71.         private string getItemBody(string entryID, string storeID, string messageClass)
  72.         {
  73.             Application outlookHandle = new Application();
  74.             NameSpace outlookNamespace = outlookHandle.GetNamespace("MAPI");
  75.             outlookNamespace.Logon(null, null, null, true);
  76.             string body;
  77.  
  78.             if (messageClass.ToLower().StartsWith("report"))
  79.             {
  80.                 ReportItem thisItem = (ReportItem)outlookNamespace.GetItemFromID(entryID, storeID);
  81.                 body = thisItem.Body;
  82.                 thisItem.Close(OlInspectorClose.olDiscard);
  83.                 //release this com reference
  84.                 int releaseResult;
  85.                 do
  86.                 {
  87.                     releaseResult = Marshal.ReleaseComObject(thisItem);
  88.                 } while (releaseResult != 0);
  89.             }
  90.             else
  91.             {
  92.                 MailItem thisItem = (MailItem)outlookNamespace.GetItemFromID(entryID, storeID);
  93.                 body = thisItem.Body;
  94.                 thisItem.Close(OlInspectorClose.olDiscard);
  95.                 //release this com reference
  96.                 int releaseResult;
  97.                 do
  98.                 {
  99.                     releaseResult = Marshal.ReleaseComObject(thisItem);
  100.                 } while (releaseResult != 0);
  101.             }
  102.  
  103.             outlookNamespace.Logoff();
  104.             outlookNamespace = null;
  105.             outlookHandle.Quit();
  106.             outlookHandle = null;
  107.  
  108.  
  109.             GC.Collect();
  110.             GC.WaitForPendingFinalizers();
  111.  
  112.             return body;
  113.         }
  114.  
  115.                     /// <summary>
  116.         /// Iterates through an Outlook.Folders object searching for a folder with the given name
  117.         /// </summary>
  118.         /// <param name="rootFolder">An Outlook.Folder object</param>
  119.         /// <param name="targetFolder"></param>
  120.         /// <returns></returns>
  121.         private Folder enumerateFolders(Folders rootFolders, string targetFolder)
  122.         {
  123.             Folder returnFolder = null;
  124.             System.Collections.IEnumerator thisEnumerator = rootFolders.GetEnumerator();
  125.             while (thisEnumerator.MoveNext())
  126.             {
  127.                 Folder f = (Folder)thisEnumerator.Current;
  128.                 string name = f.Name;
  129.                 if (targetFolder.ToLower().Equals(name.ToLower()))
  130.                 {
  131.                     returnFolder = f;
  132.                     break;
  133.                 }
  134.             }
  135.             ICustomAdapter adapter = (ICustomAdapter)thisEnumerator;
  136.             Marshal.ReleaseComObject(adapter.GetUnderlyingObject());
  137.             adapter = null;
  138.             return returnFolder;
  139.         }
  140.         }
  141.    
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement