python's smtplib cannot connect to gmail, mail.ru or anything else

…衆ロ難τιáo~ 提交于 2019-12-08 08:15:03

问题


All lines starting from line all return an error Errno 10060 or an error Errno 10061:

import smtplib
server = smtplib.SMTP('smtp.gmail.com:465')
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer = smtplib.SMTP("smtp.gmail.com", 465)
mailServer = smtplib.SMTP("smtp.mail.ru", 25)
mailServer = smtplib.SMTP("smtp.mail.ru", 2525)

Could you help? I must be missing something trivial... Error messages in full:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\lib\smtplib.py", line 302, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\lib\smtplib.py", line 277, in _get_socket
return socket.create_connection((port, host), timeout)
File "C:\Python27\lib\socket.py", line 571, in create_connection
raise err
socket.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

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\lib\smtplib.py", line 302, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\lib\smtplib.py", line 277, in _get_socket
return socket.create_connection((port, host), timeout)
File "C:\Python27\lib\socket.py", line 571, in create_connection
raise err
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it

回答1:


Use smtplib.SMTP_SSL() for a TLS connection.




回答2:


There is nothing wrong with all commands below; my issue is indeed caused by IP restrictions. Thanks for all your answers.




回答3:


Or Use server = smtplib.SMTP('smtp.gmail.com:25') for normal connection

and if you want tls from port 25, simply add

server.starttls() 

after the first line.

ps: since port 25 is default, the following is equivalent:

server = smtplib.SMTP('smtp.gmail.com')
server.starttls() 


来源:https://stackoverflow.com/questions/11933742/pythons-smtplib-cannot-connect-to-gmail-mail-ru-or-anything-else

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!