使用 Python 搜索 Outlook 收件箱

SEARCH Outlook inbox using Python

我已成功连接到我的 Outlook 收件箱并使用其修改的一些代码阅读:Reading e-mails from Outlook with Python through MAPI. What I'd like to do is search my inbox for certain email subjects. I can do this by just looping through all emails but wondering if there is a more elegant (perhaps using MAPI) way to search the inbox? I've looked through the MailItem methods 但似乎找不到任何内容。

谢谢

您发布的 link 谈论使用 Outlook 对象模型,而不是 MAPI - 扩展 MAPI 是完全不同的 API 可从 C++ 或 Delphi,但不是来自 Python.

要搜索电子邮件,请使用 Items.Find/FindNextItems.Restrict(其中 Items 集合来自 MAPIFolder.Items 属性)- 请参阅 https://msdn.microsoft.com/en-us/library/ms268869.aspx例如。

您需要使用应用程序 class 的 AdvancedSearch 方法,它允许在多个文件夹中搜索项目。项目 class 的 Restrict and Find/FindNext 方法仅允许搜索单个文件夹中的项目。

在 Outlook 中使用 AdvancedSearch 方法的主要好处是:

  • 搜索在另一个线程中执行。您不需要手动 运行 另一个线程,因为 AdvancedSearch 方法 运行 它会自动在后台运行。
  • 可以在任何位置(即超出某个文件夹的范围)搜索任何项目类型:邮件、约会、日历、便笺等。 RestrictFind/FindNext 方法可以应用于特定的项目集合(请参阅 Outlook 中文件夹 class 的项目 属性)。
  • 完全支持 DASL 查询(自定义属性也可用于搜索)。您可以在 MSDN 的 Filtering 文章中阅读更多相关信息。要提高搜索性能,如果为商店启用了即时搜索,则可以使用即时搜索关键字(请参阅商店 class 的 IsInstantSearchEnabled 属性)。
  • 您可以随时使用搜索 class 的 Stop 方法停止搜索过程。

阅读 Advanced search in Outlook programmatically: C#, VB.NET 文章中有关 AdvancedSearch 方法的更多信息。请注意,Outlook 对象模型对所有编程语言都是通用的,因此使用 Python 或 C# 并不重要。