Python SMTP 错误 10060

问题描述 投票:0回答:4

有人可以给我一些关于为什么会这样的见解吗:

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

还有这个:

mailServer = smtplib.SMTP('smtp.gmail.com:587')

说的是:

Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
File "C:\Users\user\Documents\Shuttlecock_new\python\lib\smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\user\Documents\Shuttlecock_new\python\lib\smtplib.py", line 302, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\user\Documents\Shuttlecock_new\python\lib\smtplib.py", line 277, in _get_socket
return socket.create_connection((port, host), timeout)
File "C:\Users\user\Documents\Shuttlecock_new\python\lib\socket.py", line 571, in create_connection
raise err
error: [Errno 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
  • 我在中国。
  • 我有VPN。我尝试过使用和不使用 VPN 的情况。
  • 我之前曾多次使用此代码从我的 g-mail 帐户发送电子邮件。今天突然出现这个错误。
  • 如果通过我的 Chrome 浏览器访问 Gmail,则工作正常。尽管如此,发送简单的电子邮件需要花费异常长的时间......嗯
python smtp
4个回答
10
投票

尝试指定超时(需要 Python 2.6+):

smtplib.SMTP("smtp.gmail.com", 587, timeout=120)

或尝试通过 SSL 而不是 TLS/STARTTLS 连接(带或不带

timeout
):

smtplib.SMTP_SSL("smtp.gmail.com", 465)

2
投票

我也有类似的问题。对

mailServer = smtplib.SMTP("smtp.gmail.com", 587)
的调用只是挂在那里,不执行任何操作。我什至无法退出该程序。它永远留在后台!

但是添加超时参数就解决了问题!它立即返回,我可以通过 gmail SMTP 服务器成功发送电子邮件!

但不知道为什么!


1
投票

看看我的朋友,看到这条线

mailServer = smtplib.SMTP("smtp.gmail.com", 587, timeout=120)

并将 YOUR_USERNAME 放在 587 号码后面,如下所示

mailServer = smtplib.SMTP("smtp.gmail.com", 587, "YOUR_USERNAME", timeout=120)

0
投票

您收到此错误的原因是因为您为 gmail 生成的密码在某种程度上已过期,因此只需尝试使用 gmail 中的应用程序密码重新生成另一个密码...... 我尝试过,它非常适合我的

© www.soinside.com 2019 - 2024. All rights reserved.