有人可以给我一些见解,为什么这两个:
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
答案 0 :(得分:4)
尝试指定超时(需要Python 2.6 +):
smtplib.SMTP("smtp.gmail.com", 587, timeout=120)
或尝试通过SSL而不是TLS / STARTTLS(使用和不使用timeout
)进行连接:
smtplib.SMTP_SSL("smtp.gmail.com", 465)
答案 1 :(得分:2)
我有类似的问题。拨打mailServer = smtplib.SMTP("smtp.gmail.com", 587)
只是挂在那里,没有做任何事情。我甚至不能退出该计划。它永远留在后台!
但添加超时参数解决了问题!它立即返回,我可以通过gmail SMTP服务器成功发送电子邮件!
不确定为什么!
答案 2 :(得分:1)
看看我的朋友,看看行
mailServer = smtplib.SMTP("smtp.gmail.com", 587, timeout=120)
并将YOUR_USERNAME放在587号后面,就像这样
mailServer = smtplib.SMTP("smtp.gmail.com", 587, "YOUR_USERNAME", timeout=120)