如何遍历某些Outlook子文件夹的所有MailItems

时间:2011-02-23 23:10:52

标签: c# visual-studio-2010 outlook-addin outlook-2007

我正在使用Outlook 2007加载项。我找到了一些循环遍历所有文件夹的代码,但我无法弄清楚如何在任何给定文件夹中循环 以检查MailItem对象(最终,我想要将电子邮件保存在其他位置并修改.Subject属性)。

这是我到目前为止所做的:

 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);
        }
    }

我在这个工作阶段使用了一个列表框,输出目前如下所示。我想“处理”“ Projectnnnnnn ”文件夹的电子邮件。

\\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

编辑:

我修改了上面的循环略有变化(即删除当前项目是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
    }

2 个答案:

答案 0 :(得分:3)

虽然上面的代码可能有效但很可能会遇到未处理的InvalidCastException,因为根文件夹中的所有项目都不是邮件项目(例如会议请求)。
以下代码对我有用:

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

答案 1 :(得分:2)

由于.move以下

//需要向后迭代

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);
         }
     }
 }