How to loop through all MailItems of certain Outlook subfolders

陌路散爱 提交于 2020-01-01 12:09:09

问题


I'm working on an Outlook 2007 add-in. I found some code to loop through all the folders but I have not been able to figure out how to loop inside any given folder to examine the MailItem objects (ultimately, I want to save the emails elsewhere and modify the .Subject property).

Here is what I have so far:

 private void btnFolderWalk_Click(object sender, EventArgs e)
    {
        // Retrieve the name of the top-level folder (Inbox) , for 
        // the purposes of this demonstration.
        Outlook.Folder inbox = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
          as Outlook.Folder;        // Cast the MAPI folder returned as an Outlook folder
        // Retrieve a reference to the top-level folder.
        if (inbox != null)
        {
            Outlook.Folder parent = inbox.Parent as Outlook.Folder;   // the mailbox itself
            if (parent != null)
            {
                RecurseThroughFolders(parent, 0);
            }
        }
    }
    private void RecurseThroughFolders(Outlook.Folder theRootFolder, int depth)
    {
        if (theRootFolder.DefaultItemType != Outlook.OlItemType.olMailItem)
        {
            return;
        }
        lbMail.Items.Add(theRootFolder.FolderPath);
        foreach (Object item in theRootFolder.Items)
        {
            if (item.GetType() == typeof(Outlook.MailItem))
            {
                Outlook.MailItem mi = (Outlook.MailItem)item;
                lbMail.Items.Add(mi.Subject);
            //-------------------------------------------------------------------------
            //  mi.Subject is actually a folder name as it's full path. 
            //  How to "open it" to get emails?
            //  need loop here to modify .Subject of MailItem(s) in certain subfolders
            //-------------------------------------------------------------------------
            }
        }
        foreach (Outlook.Folder folder in theRootFolder.Folders)
        {
            RecurseThroughFolders(folder, depth + 1);
        }
    }

I'm using a listbox at this stage of working things out and the output currently looks like this below. I want to "process" the email messages of the "Projectnnnnnn" folders.

\\Personal Folders
\\Personal Folders\Deleted Items
\\Personal Folders\Inbox
\\Personal Folders\Inbox\MySubFolder
\\Personal Folders\Inbox\MySubFolder\Project456212
\\Personal Folders\Inbox\MySubFolder\Project318188
\\Personal Folders\Inbox\Outbox
\\Personal Folders\Inbox\SentItems

EDIT:

I fixed this with a slight change in the loop above (i.e. removing the check that the current item is a mailitem):

foreach (Object item in theRootFolder.Items)
    {
            Outlook.MailItem mi = (Outlook.MailItem)item;
            string modifiedSubject = "Modifed Subject: " + mi.Subject;
            lbMail.Items.Add(modifiedSubject);
            mi.Subject = modifiedSubject;
            mi.Save();
     //          insert call webservice here to upload modified MailItem to new data store
    }

回答1:


While the above code may work it is likely that you will come across an unhandled InvalidCastException as not all items in the root folder will be mail items (e.g. meeting requests).
The following code worked for me:

foreach (object item in items)
{
    if (item is Outlook.MailItem)
    {
        ///The rest of your code
    }
}



回答2:


// iterating backwards is needed because of .move below

for (int i = theRootFolder.Items.Count; i > 0; i--)
{
     Outlook.MailItem mi = (Outlook.MailItem)theRootFolder.Items[i];
     if (mi != null)
     {
         if (!mi.Subject.StartsWith("M1"))
         {
             mi.Move(_TRIM_archiveFolder);
         }
     }
 }


来源:https://stackoverflow.com/questions/5098485/how-to-loop-through-all-mailitems-of-certain-outlook-subfolders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!