如何通过一定的Outlook子文件夹的所有MailItems环 [英] How to loop through all MailItems of certain Outlook subfolders

查看:1432
本文介绍了如何通过一定的Outlook子文件夹的所有MailItems环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Outlook 2007加载项。我发现一些代码来遍历所有文件夹,但我一直无法弄清楚如何循环 任何文件夹检查的MailItem对象(最后,我想保存在其他地方的电子邮件,并修改.Subject属性)。



下面是我到目前为止有:

 私人无效btnFolderWalk_Click (对象发件人,EventArgs五)
{
//检索顶层文件夹(收件箱),为
//这个演示的目的的名称。
Outlook.Folder收件箱= Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
作为Outlook.Folder; //把MAPI文件夹返回一个Outlook文件夹
//检索到顶层文件夹的参考。
如果(收件箱!= NULL)
{
Outlook.Folder父= inbox.Parent为Outlook.Folder; //邮箱本身
如果
{
RecurseThroughFolders(父母,0)(父!= NULL);
}
}
}
私人无效RecurseThroughFolders(Outlook.Folder theRootFolder,诠释深度)
{
如果(theRootFolder.DefaultItemType!= Outlook.OlItemType .olMailItem)
{
的回报;
}
lbMail.Items.Add(theRootFolder.FolderPath);
的foreach(在theRootFolder.Items对象的项目)
{
如果(item.GetType()== typeof运算(Outlook.MailItem))
{
Outlook.MailItem MI =(Outlook.MailItem)项目;
lbMail.Items.Add(mi.Subject);
// -------------------------------------------- -----------------------------
// mi.Subject实际上是一个文件夹名称,因为它的完整路径。
//如何打开来获得邮件?
//需要此处循环来在特定子
修改的MailItem(多个).Subject // ----------------------- --------------------------------------------------
}
}
的foreach(在theRootFolder.Folders Outlook.Folder夹)
{
RecurseThroughFolders(文件夹,深度+ 1);
}
}



我使用的是在这个阶段的工作列表框东西出来和输出目前看起来像下面这样。我想过程中的 Projectnnnnnn 文件夹中。

 <$的电子邮件C $ C> \\Personal文件夹
\\Personal Folders\Deleted项目
\\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(在theRootFolder.Items对象的项目)
{
Outlook.MailItem MI =(Outlook.MailItem)项目;
串modifiedSubject =体改主题:+ mi.Subject;
lbMail.Items.Add(modifiedSubject);
mi.Subject = modifiedSubject;
mi.Save();这里
//调用插入Web网页上传修改的MailItem到新的数据存储
}


解决方案

虽然上面的代码可能工作很可能你会遇到一个未处理InvalidCastException的在根文件夹不是所有项目将邮寄物品(如会议请求)。结果
下面的代码为我工作:

 的foreach(对象中的项目项)
{
如果(产品Outlook.MailItem)
{
///代码
其余}
}


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
    }

解决方案

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

这篇关于如何通过一定的Outlook子文件夹的所有MailItems环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆