是否可以使用python对来自特定发件人的Outlook收件箱中的所有电子邮件进行排序?

我需要对公司的Outlook帐户进行排序,并希望对通过特定地址发送的所有电子邮件进行排序,并找到对该电子邮件的所有答复。我的理解是,我可以使用win32com.client模块访问我的Outlook,并能够读取该文件夹中的所有“未读”电子邮件。但是,我想更改过滤器以根据特定发件人阅读电子邮件。我似乎找不到比在messages对象上可以调用的方法更全面的列表。您可以指定发件人吗?

到目前为止,这是我的代码:

import win32com.client

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

inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
                                    # the inbox. You can change that number to reference
                                    # any other folder
messages = inbox.Items
messages.Sort("[ReceivedTime]",True)

sender = 'xxxxx@xxxx.com'
for message in messages:
    if sender in message:
        print (message.body)
评论
  • 情侣~装
    情侣~装 回复

    Iterating over all items in a folder is not really a good idea. Instead, you need to use the Find/FindNext or Restrict methods of the Items class in Outlook. Read more about them in the following articles:

    例如,您可以使用以下搜索条件:

    outItems = Items.Restrict("[SenderEmailAddress] = " & "'" & address & "'")
    

    Also, you may find the AdvancedSearch method of the Application class helpful. The key benefits of using the `AdvancedSearch method in Outlook are:

    • 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.
    • Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. 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).
    • Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in 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).
    • You can stop the search process at any moment using the Stop method of the Search class.