使用 Python 连接到 Exchange 邮箱

问题描述

我需要在 Python 脚本中连接到 Exchange 邮箱,而不使用本地计算机上的任何配置文件设置(包括使用 Outlook).如果我使用 win32com 创建 MAPI.Session 我可以使用现有配置文件登录(使用 Logon() 方法),但我只想提供用户名和密码.

I need to connect to an Exchange mailbox in a Python script, without using any profile setup on the local machine (including using Outlook). If I use win32com to create a MAPI.Session I could logon (with the Logon() method) with an existing profile, but I want to just provide a username & password.

这可能吗?如果是这样,有人可以提供示例代码吗?如果它只使用标准库和 pywin32 包,我会更喜欢.遗憾的是,无法为 Exchange 服务器启用 IMAP 访问(然后使用 imaplib).

Is this possible? If so, could someone provide example code? I would prefer if it only used the standard library and the pywin32 package. Unfortunately, enabling IMAP access for the Exchange server (and then using imaplib) is not possible.

如果有必要:所有脚本将做的就是连接到邮箱,并运行收件箱中的邮件,检索内容.如果我能首先获得连接,我可以处理为此编写代码!

In case it is necessary: all the script will be doing is connecting to the mailbox, and running through the messages in the Inbox, retrieving the contents. I can handle writing the code for that, if I can get a connection in the first place!

澄清有关 Outlook:Outlook 将安装在本地计算机上,但它没有任何帐户设置(即所有适当的库都将可用,但我需要独立于 Outlook 内部的任何设置进行操作).

To clarify regarding Outlook: Outlook will be installed on the local machine, but it does not have any accounts setup (i.e. all the appropriate libraries will be available, but I need to operate independently from anything setup inside of Outlook).


解决方案

我很确定如果不使用 Outlook 和 MAPI 配置文件,这将是不可能的.如果您能与您的邮件管理员甜言蜜语,让您在 Exchange 服务器上启用 IMAP,那么您的生活会轻松很多.

I'm pretty sure this is going to be impossible without using Outlook and a MAPI profile. If you can sweet talk your mail admin into enabling IMAP on the Exchange server it would make your life a lot easier.

相关文章