python - 在 python 2.7(不是 3)中使用 starttls 连接到 IMAP Exchange

标签 python python-2.7 starttls

我刚刚发现(不确定)python 3 支持 starttls connect 但不支持 python 2.7

但是我当前的大型 python 代码在 python 2.7 上运行,因此无法立即转移到 3。

那么,我可以使用 imap 和 starttls 安全性连接到 Microsoft Exchange 服务器吗?

需要更多详细信息?

最佳答案

已经过去了一段时间,但我希望这对其他人有帮助。您可以使用 imaplib 和 Python 3 或 Python 2 通过 TLS 连接到 Exchange。

Python 3 有 IMAP4.starttls() 方法,Python 2 没有。对于 Python 2,请改用 IMAP4_SSL 子类。以下代码片段来自通过 TLS 连接到 office365.com 的工作解决方案。

Python 3

from imaplib import IMAP4
. . .

    def _connect(self, host, username, password, use_tls=False):
        """Establishes an IMAP connection, optionally over TLS."""
        try:
            src = IMAP4(host)
            if use_tls:
                src.starttls()
            src.login(username, password)
        except . . .

Python 2

from imaplib import IMAP4, IMAP4_SSL
. . .

    def _connect(self, host, username, password, use_tls=False):
        """Establishes the IMAP connection, optionally over TLS."""
        try:
            src = IMAP4_SSL(host) if use_tls else IMAP4(host)
            src.login(username, password)
        except . . .

关于python - 在 python 2.7(不是 3)中使用 starttls 连接到 IMAP Exchange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28214673/

相关文章:

Python 将一个文件的内容复制到另一个目录中的另一个文件中

python - python中是否存在空类?

php - 无法使用 PHP 发送电子邮件

java - SMTP STARTTLS : how to behave if remote SMTP server has self-signed certificate?

ssl - 带有使用 Spring 容器的自签名证书的 Javamail STARTTLS

python - 强制 Pandas 保留多个同名列

python - 如何将 Django-CMS 集成到现有项目中

c++ - 如何将 C++ 类与 ctypes 一起使用?

python - 如何按两列或更多列对 python pandas 中的数据帧进行排序?

python-2.7 - 如何使用 Opencv 直接从 Azure Blob 存储读取图像,而不将其下载到本地文件?