Py学习  »  Python

无法在python中使用imap连接gmail/outlook

Sounak Banerjee • 4 年前 • 1089 次点击  

我已经尝试使用chilkat包在spyder(python 3.6)中使用imap连接gmail服务器。 我已为中的所有邮件启用了IMAP 设置>转发和POP/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")

这个 success 变量是 boolean 仍然是假的。请帮帮我。我的目标是从outlook server获取所有附件并将其转储到一个文件中,但我甚至无法连接到gmail服务器。我试图使用“imap.mail.outlook.com”,但也失败了。我不知道在outlook中启用imap的步骤。但即使在gmail中启用了它,为什么它不能工作呢?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40039
 
1089 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Matt
Reply   •   1 楼
Matt    5 年前

第一步是检查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下载完全相同的方式保存附件。