我有一个 python 脚本,可以从我的收件箱中获取最近的邮件,它工作正常,但问题是它会获取旧邮件作为最新的电子邮件。
我已经同步了我的 Outlook 应用程序,它现在也显示了我最近的所有电子邮件,但 python 脚本在同步之前继续获取旧邮件。
我还卸载了 Outlook 应用程序并从 Microsoft 商店下载了新的应用程序,但脚本显示的是相同的旧邮件。我卸载了 pywin32,希望与 Outlook 应用程序建立新的连接,但没有任何结果。
这是我的 python 脚本:
import win32com.client #pip install pywin32 if not installed
# Connect to Outlook by MAPI
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
message = messages.GetFirst()
body_content = message.body
sender = message.sender
subject = message.Subject
date = message.senton.date()
time = message.senton.time()
attachments = message.Attachments
print(body_content)
问题出在我的脚本上吗?如何使该脚本与我的 Outlook 应用程序同步?
最佳答案
我添加了 Items.sort() 并且它起作用了!
这就是我在阅读@Devanshu的评论后所做的
messages = inbox.Items
messages.Sort("[ReceivedTime]", True)
我在获取项目之前调用了排序方法。
关于Python 脚本无法从 Outlook 获取最新邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54668793/