0

我尝试使用 Chilkat 包在 Spyder(Python 3.6)中使用 IMAP 连接到 Gmail 服务器。我在“设置”>“转发”和“POP/IMAP”中为所有邮件启用了 IMAP ,然后我还在此处启用了不太安全的应用程序选项卡https://myaccount.google.com/lesssecureapps?pli=1登录后。但在这段代码

import sys
import chilkat

imap = chilkat.CkImap()

#  Anything unlocks the component and begins a fully-functional 30-day trial.
success = imap.UnlockComponent("Anything for 30-day trial")
if (success != True):
    print(imap.lastErrorText())
    sys.exit()

#  Connect to an IMAP server.
#  Use TLS
imap.put_Ssl(True)
imap.put_Port(993)
success = imap.Connect("imap.gmail.com")

为 a的success变量boolean仍然为 False。请帮我。我的目标是从 Outlook Server 获取所有附件并将它们转储到一个文件中。但我什至无法连接到 Gmail 服务器。我尝试使用“imap.mail.Outlook.com”,但也失败了。我不知道在 Outlook 中启用 IMAP 的步骤。但是即使在 Gmail 中启用了它,为什么它不起作用?

4

1 回答 1

1

第一步是检查 imap.LastErrorText 属性的内容以查看发生了什么。例如:

#  Connect to an IMAP server.
#  Use TLS
imap.put_Ssl(True)
imap.put_Port(993)
success = imap.Connect("imap.someMailServer.com")
if (success != True):
    print(imap.lastErrorText())
    sys.exit()

我的猜测是防火墙(软件或硬件)阻止了出站连接。

另一种解决方案是使用 GMail REST API,如以下示例所示: https://www.example-code.com/python/gmail.asp HTTP 端口 (443) 不太可能被防火墙阻止。您将下载到 Chilkat 电子邮件对象,然后以与通过 IMAP 下载完全相同的方式保存附件。

于 2018-11-23T20:16:06.253 回答