[英]How to search an email from all folders in store using Linq Outlook C#
I am performing custom action on all incoming replies through VSTO add-in.我正在通过 VSTO 加载项对所有传入回复执行自定义操作。 The add-in will compare ConversationID of incoming reply with existing email.
加载项将传入回复的 ConversationID 与现有电子邮件进行比较。 It works fine if I have to search inside one folder but my problem is email can be in any folder in store.
如果我必须在一个文件夹中搜索,它工作正常,但我的问题是电子邮件可以在商店中的任何文件夹中。 Here is my code.
这是我的代码。
void items_ItemAdd(object Item)
{
Outlook.Application application = new Outlook.Application();
string filter = "RE: ";
Outlook.MailItem mail = (Outlook.MailItem)Item;
Outlook.Folder folder = mail.Parent as Outlook.Folder;
if (Item != null)
{
if (mail.MessageClass == "IPM.Note" && mail.Subject.ToUpper().Contains(filter.ToUpper()))
{
var RequiredMail = (from e in folder.Items.Cast<Outlook.MailItem>().OrderBy(X => X.ReceivedTime).Where(C => C.ConversationID == mail.ConversationID) select mail).FirstOrDefault();
// Perform custom action
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Also, I have read that searching for an email using Linq is not very efficient.另外,我读到使用 Linq 搜索电子邮件效率不高。 Is there any other more efficient way to get
RequiredMail
?还有其他更有效的方法来获取
RequiredMail
吗?
Any help will be highly appreciated.任何帮助将不胜感激。
Thank you.谢谢你。
First of all, you must be aware that ItemAdd
event may not be fired if more than sixteen items are added to the collection.首先,您必须知道,如果向集合中添加了 16 个以上的项目,则可能不会触发
ItemAdd
事件。 This is a known issue in Outlook.这是 Outlook 中的一个已知问题。 The following series of articles describes possible workarounds for that:
以下系列文章描述了可能的解决方法:
Mixing LINQ and COM objects is not a really good idea.混合使用 LINQ 和 COM 对象并不是一个好主意。 You should release underlying COM objects instantly to prevent any known issues.
您应该立即释放底层 COM 对象以防止出现任何已知问题。
If you need to search for items in all folders you may use the AdvancedSearch
method of the Application class which allows to perform a search based on a specified DAV Searching and Locating (DASL) search string.如果您需要搜索所有文件夹中的项目,您可以使用 Application 类的
AdvancedSearch
方法,该方法允许根据指定的 DAV 搜索和定位 (DASL) 搜索字符串执行搜索。
The key benefits of using the AdvancedSearch method in Outlook are:在 Outlook 中使用AdvancedSearch方法的主要好处是:
AdvancedSearch
method runs it automatically in the background.AdvancedSearch
方法会在后台自动运行它。Restrict
and Find
/ FindNext
methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook). Restrict
和Find
/ FindNext
方法可以应用于特定的 Items 集合(请参阅 Outlook 中 Folder 类的 Items 属性)。IsInstantSearchEnabled
property of the Store class).IsInstantSearchEnabled
属性)。Stop
method of the Search
class.Search
类的Stop
方法停止搜索过程。 Read more about that in the Advanced search in Outlook programmatically: C#, VB.NET article. 在 Outlook 中以编程方式进行高级搜索:C#、VB.NET文章中阅读有关此内容的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.