有人可以给我一些关于为什么会这样的见解吗:
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
尝试指定超时(需要 Python 2.6+):
smtplib.SMTP("smtp.gmail.com", 587, timeout=120)
或尝试通过 SSL 而不是 TLS/STARTTLS 连接(带或不带
timeout
):
smtplib.SMTP_SSL("smtp.gmail.com", 465)
我也有类似的问题。对
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
的调用只是挂在那里,不执行任何操作。我什至无法退出该程序。它永远留在后台!
但是添加超时参数就解决了问题!它立即返回,我可以通过 gmail SMTP 服务器成功发送电子邮件!
但不知道为什么!
看看我的朋友,看到这条线
mailServer = smtplib.SMTP("smtp.gmail.com", 587, timeout=120)
并将 YOUR_USERNAME 放在 587 号码后面,如下所示
mailServer = smtplib.SMTP("smtp.gmail.com", 587, "YOUR_USERNAME", timeout=120)
您收到此错误的原因是因为您为 gmail 生成的密码在某种程度上已过期,因此只需尝试使用 gmail 中的应用程序密码重新生成另一个密码...... 我尝试过,它非常适合我的