python - 连接重置错误: [WinError 10054] An existing connection was forcibly closed by the remote host (while accessing Gmail emails)

标签 python gmail imap

当我尝试使用 python 的 imap-tools 模块访问我的 gmail 电子邮件时,出现标题错误。以下是建立连接期间给出的详细信息。

    Traceback (most recent call last):
  File "d:\PythonLabs\delete_gmail_emails.py", line 3, in <module>
    gautham_gmail_box = MailBox('imap.gmail.com')
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\imap_tools\main.py", line 61, in __init__
    host, port or imaplib.IMAP4_SSL_PORT, keyfile, certfile, ssl_context)
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\imaplib.py", line 1283, in __init__
    IMAP4.__init__(self, host, port)
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\imaplib.py", line 197, in __init__
    self.open(host, port)
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\imaplib.py", line 1296, in open
    IMAP4.open(self, host, port)
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\imaplib.py", line 294, in open
    self.sock = self._create_socket()
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\imaplib.py", line 1288, in _create_socket
    server_hostname=self.host)
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 412, in wrap_socket
    session=session
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 850, in _create
    self.do_handshake()
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 1108, in do_handshake
    self._sslobj.do_handshake()
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

[Done] exited with code=1 in 0.338 seconds

我认为该错误是因为我的 gmail 电子邮件受到两步身份验证的保护。我想保证我的 Gmail 帐户的安全,同样想知道从 python 程序访问我的电子邮件的解决方法。

这是我的Python代码:

    from imap_tools import MailBox

gautham_gmail_box = MailBox('imap.gmail.com')
gautham_gmail_box.login('my_email@gmail.com','my_password')

最佳答案

我认为您必须先启用对 Gmail 的某些不安全访问,然后才能通过这种方式访问​​它,但我不确定。

IMO,您最好使用 Google 的官方库来使用 Python 访问 Gmail。访问https://developers.google.com/gmail/api/quickstart/python了解更多相关信息。

关于python - 连接重置错误: [WinError 10054] An existing connection was forcibly closed by the remote host (while accessing Gmail emails),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52759754/

相关文章:

python - 显示从文件中读取的 unicode

php - 使用gmail smtp服务器发送电子邮件时smtp.gmail.com vs ssl ://smtp. gmail.com

PHP Imap_mail_move() 使 "Seen"消息为 "Unread"

c# - 寻找新电子邮件的实时IMAP通知

oauth - 通过 GMail XOAUTH 出现 IMAP "Invalid Credentials"错误

python - sqlalchemy session 执行搞乱了有效的 postgres 语法

python - 使用 python 修复格式错误的 XML

python - AWS boto3 Athena 查询结果未保存到本地路径

google-chrome - Chrome 用户脚本 (Greasemonkey) - 阻止 Gmail 发送电子邮件

java - 如何打印 Spring Message 捕获的电子邮件正文内容