如何循环访问某些 Outlook 子文件夹的所有 MailItem

发布于 2024-10-18 14:58:32 字数 2789 浏览 7 评论 0原文

我正在开发 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

编辑:

我通过对上面的循环进行轻微更改来修复此问题(即删除当前项目是邮件项目的检查):

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
    }

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
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情废物 2024-10-25 14:58:32

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

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

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
    }
}
自演自醉 2024-10-25 14:58:32

// 由于下面的 .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);
         }
     }
 }

// 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);
         }
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文