简体   繁体   English

如何使用 Linq Outlook C# 从商店中的所有文件夹中搜索电子邮件

[英]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方法的主要好处是:

  • The search is performed in another thread.搜索在另一个线程中执行。 You don't need to run another thread manually since the AdvancedSearch method runs it automatically in the background.您不需要手动运行另一个线程,因为AdvancedSearch方法会在后台自动运行它。
  • Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, ie beyond the scope of a certain folder.可以在任何位置搜索任何项目类型:邮件、约会、日历、笔记等,即超出某个文件夹的范围。 The Restrict and Find / FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook). RestrictFind / FindNext方法可以应用于特定的 Items 集合(请参阅 Outlook 中 Folder 类的 Items 属性)。
  • Full support for DASL queries (custom properties can be used for searching too).完全支持 DASL 查询(自定义属性也可用于搜索)。 You can read more about this in the Filtering article in MSDN.您可以在 MSDN 的过滤文章中阅读有关此内容的更多信息。 To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).为了提高搜索性能,如果为商店启用了即时搜索,则可以使用即时搜索关键字(请参阅 Store 类的IsInstantSearchEnabled属性)。
  • You can stop the search process at any moment using the 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM