gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:45:45 27 4
gpt4 key购买 nike

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

但是我当前的大型 python 代码在 python 2.7 上运行,所以不能立即转移到 3。

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

需要更多详细信息?

最佳答案

一段时间过去了,但我希望这对其他人有帮助。您可以使用带有 Python 3 或 Python 2 的 imaplib 通过 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com