简体   繁体   English

如何通过c#在所有Outlook共享邮箱文件夹中一次“搜索所有邮箱”?

[英]How do I “Search All Mailboxes” in all Outlook Shared Mailbox Folders at once via c#?

I want to mimic the latest version of Outlook's search functionality via c# and a windows form. 我想通过c#和Windows窗体模仿Outlook搜索功能的最新版本。 Specifically, I want to "Search All Mailboxes" for a given string. 具体来说,我想“搜索所有邮箱”中给定的字符串。 There are over 50 folders and 90,000 emails. 有超过50个文件夹和90,000封电子邮件。

Currently, I am able to search any one folder using LINQ and get results. 目前,我可以使用LINQ搜索任何一个文件夹并获得结果。 I wrote some code to iterate through all the folders and create one massive IEnumberable that I can query. 我编写了一些代码来遍历所有文件夹,并创建一个可以查询的大型IEnumberable。

public IEnumerable<MailItem> SharedInbox
    {

        get
        {
            outlook.ActiveExplorer().Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            Outlook.Recipient recip = Outlook.Application.Session.CreateRecipient("TOCCard@Capitalone.Com");
            Microsoft.Office.Interop.Outlook.MAPIFolder folder =
                    outlook.GetNamespace("MAPI").GetSharedDefaultFolder(recip, OlDefaultFolders.olFolderInbox);
            Folders subFolders = folder.Folders;
            IEnumerable<MailItem> mItems  = folder.Folders[1].Items.OfType<MailItem>();

            if (recip.Resolve())
            {
                System.Diagnostics.Debug.WriteLine("Email Address Resolve Successful.\r\n");
                try
                {
                    foreach (MAPIFolder fold in subFolders)
                    {
                        System.Diagnostics.Debug.WriteLine("Try Folder: " + fold.Name + " \r\n");
                        try
                        {
                           mItems = mItems.Concat(fold.Items.OfType<MailItem>());
                        }
                        catch
                        { System.Diagnostics.Debug.WriteLine("No items found:\r\n"); }
                    }


                    return mItems;
                }
                catch
                {
                    return null;
                }

            }
            else
            {
                System.Diagnostics.Debug.WriteLine("ELSE");
                return null;

            }

        }

This eventually works, but you can probably imagine that this excruciatingly slow and as such is useless. 最终这是可行的,但您可能会想象这太慢了,因此毫无用处。 I am new to LINQ and I feel like there must be a faster way. 我是LINQ的新手,我觉得必须有一个更快的方法。 Can this code be adjusted? 可以调整此代码吗? I am not an Exchange admin and have no access to the exchange servers beyond my own inbox. 我不是Exchange管理员,除了我自己的收件箱外,无法访问Exchange服务器。 Also I am not married to LINQ and would be happy to use other methods. 另外,我还没有嫁给LINQ,所以很乐意使用其他方法。 I will appreciate your help. 感谢您的帮助。

NOTE: I just noticed that the above code is going through folder[1] twice. 注意:我只是注意到上面的代码两次通过folder [1]。 I can fix that, but it is not significantly affecting the time it takes. 我可以解决此问题,但不会显着影响所需的时间。

No not use LINQ with OOM. 不能不将LINQ与OOM一起使用。 Use Items.Find/FindNext or Items.Restrict . 使用Items.Find/FindNextItems.Restrict

You can also use Aplication.AdvancedSearch (keep in mind it is asynchronous). 您还可以使用Aplication.AdvancedSearch (请注意,它是异步的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Linq Outlook C# 从商店中的所有文件夹中搜索电子邮件 - How to search an email from all folders in store using Linq Outlook C# 如何使用 C# 更新所有 Outlook(2003) 文件夹 - how to Update All Outlook(2003) Folders using C# 使用ExchangeService C#在Outlook帐户中搜索邮箱 - Search Mailboxes in an Outlook Account using ExchangeService C# C# 按主题在所有 Outlook 文件夹中搜索 MailItem - C# Searching All Outlook Folders for MailItem by Subject C#Outlook共享邮箱间歇性获得子文件夹 - C# Outlook Shared Mailbox get subfolders intermittent 从Outlook C#中的“共享邮箱”获取用户定义的类别 - Get user defined categories from Shared Mailbox in Outlook C# C# 如何从 Outlook 的共享邮箱发送邮件并将其保存在已发送文件夹中 - C# How to send mail from Outlook's Shared Mailbox and keep it in Sent folder there 如何列出和创建所有 IIS 网站、网站中的文件夹和 C# 中的虚拟 web 目录 - How do I list and create all IIS websites, folders in the websites and virtual web directories in C# 如何通过c#在Outlook中创建一组联系人文件夹 - How to create a group of contact folders in outlook via c# 如何通过 C# 中的图表 API 更新用户的邮箱时区 - How do I update a user's mailbox timezone via Graph API in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM