[英]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/FindNext
或Items.Restrict
。
You can also use Aplication.AdvancedSearch
(keep in mind it is asynchronous). 您还可以使用
Aplication.AdvancedSearch
(请注意,它是异步的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.