如何使用Python按相反顺序浏览Outlook电子邮件
我想阅读我的Outlook电子邮件,只想阅读未读的邮件。我现在掌握的代码是:如何使用Python按相反顺序浏览Outlook电子邮件,python,win32com,Python,Win32com,我想阅读我的Outlook电子邮件,只想阅读未读的邮件。我现在掌握的代码是: import win32com.client outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") inbox = outlook.GetDefaultFolder(6) messages = inbox.Items message = messages.GetFirst () while message:
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
message = messages.GetFirst ()
while message:
if message.Unread == True:
print (message.body)
message = messages.GetNext ()
但这是从第一封邮件到最后一封邮件。我想按相反的顺序去,因为未读的电子邮件将在顶部。有办法吗?为什么不使用for循环?从头到尾浏览你的信息,就像你试图做的那样
for message in messages:
if message.Unread == True:
print (message.body)
我同意科尔的观点,一个for循环有助于完成所有这些任务。如果从最近收到的电子邮件开始是很重要的(例如,对于特定订单,或限制您通过的电子邮件数量),您可以使用该功能按属性对其进行排序
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
#the Sort function will sort your messages by their ReceivedTime property, from the most recently received to the oldest.
#If you use False instead of True, it will sort in the opposite direction: ascending order, from the oldest to the most recent.
messages.Sort("[ReceivedTime]", True)
for message in messages:
if message.Unread == True:
print (message.body)
这不就是改变message=messages.GetFirst()吗?GetLast()如果存在,或者寻找一个函数来执行类似的操作是的,有一个
GetLast
和一个GetPrevious
方法。如何以相反的顺序获取它们应该是不言而喻的…GetLast()
和GetNext()
不能一起工作@OmidCompSCI,我找不到GetPrevious()
。谢谢你@kindall