【问题标题】:Find specific mail in Outlook by title按标题在 Outlook 中查找特定邮件
【发布时间】:2020-02-08 20:30:43
【问题描述】:

我正在尝试创建一个代码,该代码将使用主题字段在我的邮箱中查找信件(例如,根据 Outlook 规则,这些信件将位于文件夹“TODO”中)。 这就是我现在所拥有的:

import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

inbox = outlook.GetDefaultFolder(6).Folders.Item("TODO")

messages = inbox.Items
message = messages.GetLast()
body_content = message.subject
print(body_content)

此代码查找文件夹中的最后一个字母。

提前谢谢你。

【问题讨论】:

    标签: python outlook win32com com-automation


    【解决方案1】:

    下面的代码将在TODO文件夹的所有消息中搜索,如果主题与要搜索的字符串匹配,它将打印找到的消息

    import win32com.client
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    
    inbox = outlook.GetDefaultFolder(6).Folders.Item("TODO")
    
    messages = inbox.Items
        for message in messages:
            if message.subject == 'String To be Searched':
                print("Found message")
    

    【讨论】:

    • 返回错误:如果 message.subjectis 在 'S2S scale run for' 中:^ SyntaxError: invalid syntax
    • 已将 'is in' 替换为 '==',现在可以使用了,谢谢!
    • 你能接受这个答案吗?这有助于社区!
    【解决方案2】:

    您需要使用Items 类的Find/FindNextRestrict 方法。例如,要获取带有Hello world 主题行的项目,您可以使用以下代码:

    private void FindAllUnreadEmails(Outlook.MAPIFolder folder)
    {
        string searchCriteria = "[Subject] = `Hello world`";
        StringBuilder strBuilder = null;
        int counter = default(int);
        Outlook._MailItem mail = null;
        Outlook.Items folderItems = null;
        object resultItem = null;
        try
        {
            if (folder.UnReadItemCount > 0)
            {
                strBuilder = new StringBuilder();
                folderItems = folder.Items;
                resultItem = folderItems.Find(searchCriteria);
                while (resultItem != null)
                {
                    if (resultItem is Outlook._MailItem)
                    {
                        counter++;
                        mail = resultItem as Outlook._MailItem;
                        strBuilder.AppendLine("#" + counter.ToString() + 
                                              "\tSubject: " + mail.Subject);
                    }
                    Marshal.ReleaseComObject(resultItem);
                    resultItem = folderItems.FindNext();
                }
                if (strBuilder != null)
                    Debug.WriteLine(strBuilder.ToString());
            }
            else
                Debug.WriteLine("There is no match in the " 
                                     + folder.Name + " folder.");
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
        finally
        {
            if (folderItems != null) Marshal.ReleaseComObject(folderItems);
        }
    }
    

    在以下文章中详细了解这些方法:

    此外,您可能会发现 Application 类的 AdvancedSearch 方法很有帮助。在 Outlook 中使用 AdvancedSearch 方法的主要好处是:

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

    Advanced search in Outlook programmatically: C#, VB.NET 文章中了解有关此方法的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      相关资源
      最近更新 更多