使用python和win32com读取Outlook电子邮件时身份验证的工作原理

时间:2017-10-06 10:35:22

标签: python email outlook win32com

我知道我们可以使用以下代码(Reading e-mails from Outlook with Python through MAPI)来阅读outlook中的电子邮件。

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
message = messages.GetLast()
body_content = message.body
print body_content

但我们并未在上述代码中的任何位置提供用户名和密码。 那么代码是如何验证Outlook帐户的呢。

任何人都可以解释身份验证是如何发生的。

1 个答案:

答案 0 :(得分:3)

win32com.client正在与Outlook COM object进行互动。由于 Outlook singleton,因此您实际上正在生成 Outlook 的“隐藏”实例。请记住,每次登录Outlook时都不需要输入用户名和密码。这就是为什么这里不需要用户名和密码的原因。

此外,只要打开Outlook的COM对象,您将无法通过“exlporer”打开Outlook。这是因为只允许一个Outlook实例。您可能会注意到,虽然您从未打开过Outlook的GUI,但仍然会收到新电子邮件的弹出消息。