试图发送带有smtplib的电子邮件的TimeoutError

时间:2017-04-26 04:55:54

标签: python smtplib

我正在尝试使用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

2 个答案:

答案 0 :(得分:1)

看起来你没有启用"允许更少的安全应用"您的GMail帐户中的选项。

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

答案 1 :(得分:0)

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