【问题标题】:TimeoutError trying to send an email with smtplibTimeoutError 尝试使用 smtplib 发送电子邮件
【发布时间】:2017-04-26 04:55:54
【问题描述】:

我正在尝试使用 Python 的 smtplib 模块发送电子邮件,但我一直收到TimeoutError

import smtplib

def send():
    try:
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.ehlo()
        server.starttls()
        server.login(email, password)  # Email and password are already defined
        print('Enter recipient: ')
        recipient = input()
        print('Enter subject:')
        subject = input() + '\n'
        print('Enter body:')
        body = input()
        server.sendmail(email, recipient, body)
        server.quit()

    except TimeoutError as e:
        print (str(e))

此代码收到:

[WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

【问题讨论】:

  • 可能是连接问题或防火墙?

标签: python smtplib


【解决方案1】:

您的 GMail 帐户中似乎没有启用“允许不太安全的应用程序”选项。

这是来自 Google 的更正式的内容, https://support.google.com/accounts/answer/6010255?hl=en

【讨论】:

  • 我已启用此设置,但仍无法连接。我正在考虑是否尝试增加我的超时范围: import socket socket.setdefaulttimeout(120) 这可能有效吗?
  • @user7722705:不要问,试试看!
【解决方案2】:

如果您使用的是 VPN 连接,请先禁用它。它对我有用。

【讨论】:

  • 您可以与我们分享您的结果。一张小图可能就够了
【解决方案3】:

使用端口 587。我遇到了同样的错误,但是当我输入 587 作为端口时它工作了

smtplib.SMTP("smtp.gmail.com", 587)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-09
    • 2015-09-07
    • 2021-04-26
    • 1970-01-01
    • 2014-01-31
    • 2015-03-06
    相关资源
    最近更新 更多