简体   繁体   English

无法在python中使用IMAP连接到gmail / Outlook

[英]Unable to connect to gmail/Outlook using IMAP in python

I have tried to connect to the Gmail server using IMAP in Spyder(Python 3.6) using the Chilkat package. 我尝试使用Chilkat软件包在Spyder(Python 3.6)中使用IMAP连接到Gmail服务器。 I have enabled the IMAP for all Mail in the Settings>Forwarding and POP/IMAP and then I have also enabled the less secure apps tab here https://myaccount.google.com/lesssecureapps?pli=1 after signing in. But in this code 登录后,我已在“ 设置”>“转发”和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")

The success variable which is a boolean remains False. boolean success变量保持为False。 Please help me. 请帮我。 My aim is to fetch all attachments from Outlook Server and dump them into a file.But I cannot even connect to the Gmail server. 我的目的是从Outlook Server提取所有附件并将其转储到文件中。但是我什至无法连接到Gmail服务器。 I tried to use "imap.mail.Outlook.com" but that also failed. 我尝试使用“ imap.mail.Outlook.com”,但是也失败了。 I do not know the steps to enable IMAP in Outlook. 我不知道在Outlook中启用IMAP的步骤。 But even if it is enabled in Gmail, why is it not working? 但是,即使在Gmail中启用了它,为什么也不能正常工作呢?

The 1st step is to examine the contents of the imap.LastErrorText property to see what happened. 第一步是检查imap.LastErrorText属性的内容以查看发生了什么。 For example: 例如:

#  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()

My guess is that a firewall (software or hardware) is blocking the outbound connection. 我的猜测是防火墙(软件或硬件)阻止了出站连接。

An alternative solution is to use the GMail REST API as shown in these examples: https://www.example-code.com/python/gmail.asp The HTTP ports (443) are unlikely to be blocked by a firewall. 另一种解决方案是使用GMail REST API,如以下示例所示: https : //www.example-code.com/python/gmail.asp HTTP端口(443)不太可能被防火墙阻止。 You would download into a Chilkat Email object and then save attachments in exactly the same way had you downloaded via IMAP. 您将下载到Chilkat电子邮件对象中,然后以与通过IMAP下载时完全相同的方式保存附件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM