Python SMTP 错误 10060
有没有人能告诉我,为什么这两段代码:
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的情况。
- 我之前很多次都用这段代码从我的Gmail账户发送邮件,今天突然出现了这个错误。
- 通过我的Chrome浏览器访问Gmail一切正常。不过,发送简单邮件的速度异常慢……嗯。
5 个回答
1
看吧,我的朋友,看看这一行
mailServer = smtplib.SMTP("smtp.gmail.com", 587, timeout=120)
然后在587这个数字后面加上你的用户名,像这样
mailServer = smtplib.SMTP("smtp.gmail.com", 587, "YOUR_USERNAME", timeout=120)
2
我遇到过类似的问题。当我调用 mailServer = smtplib.SMTP("smtp.gmail.com", 587)
时,程序就卡住了,什么都不动。我甚至无法退出程序,它一直在后台运行,永远也停不下来!
不过,添加一个超时参数就解决了这个问题!这样程序会立刻返回,我也能成功通过 Gmail 的 SMTP 服务器发送邮件了!
不过我也不太明白为什么会这样!
10
试着设置一个超时时间(需要使用Python 2.6或更高版本):
smtplib.SMTP("smtp.gmail.com", 587, timeout=120)
或者可以尝试通过SSL连接,而不是使用TLS/STARTTLS(可以选择有或没有timeout
):
smtplib.SMTP_SSL("smtp.gmail.com", 465)