Sunday, March 2, 2014

Reading Outlook 2010 Folders - Is there a better way to do this?

Hello Everyone:


My goals are:



  1. Use C# to find ALL mail folders in users Outlook ap

  2. Read and display ALL emails in the C# Application


I'm working on goal one right now. Following is my script:



private void All_Mail_Btn_Click(object sender, EventArgs e)
{
OutLook.Application app = new OutLook.Application();
OutLook.NameSpace ns = app.GetNamespace("MAPI");

foreach (Folder folder in ns.Folders)
{
GetFolders(folder);
}
}

And



public void GetFolders(MAPIFolder folder)
{
string mailFldr = folder.Name.ToString();
if (mailFldr == "Inbox" || mailFldr.StartsWith("Sent"))
{
Console.WriteLine("mail Folder Name: " + folder.Name + " ... Count: " + folder.Items.Count);
}
if (folder.Folders.Count > 0)
{
foreach (MAPIFolder subFolder in folder.Folders)
{
GetFolders(subFolder);
}

}

}

So... is this the most efficient way to read ONLY mail folders (and ALL inbox/sent folders)?


I'm asking - because it seems sort of convoluted to me to run GetFolders(subFolder) inside GetFolders(MAPIFolder folder)?


From experimentation, I can tell that the first round processes the root folders and the foreach (MAPIFolder subFolder in folder.Folders) processes subfolders. So ... in a way it makes sense.


But honestly - Accessing Outlook folders has to be a common need within Visual Studio, C# and VB ... is there no other way to do this??


In addition I understand MAPI is being depreciated ... so that is another reason I'm wondering if there is a more efficient way?


Any feedback would be appreciated.


Thanks in advance - Pavilion


No comments:

Post a Comment