问题
Trying to connect to smpt server of gmail but it is giving network unreachable
import smtplib
s=smtplib.SMTP('smtp.gmail.com',587)
Neither this is working
import smtplib
s=smtplib.SMTP_SSL('smtp.gmail.com',587)
It is giving following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 311, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 286, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 101] Network is unreachable
回答1:
Use port 465. From Google docs on SMTP configuration;
import smtplib
s = smtplib.SMTP_SSL('smtp.gmail.com', 465)
回答2:
According to this page port 587 requires TLS.
回答3:
For me when i tried in Ubuntu it is working with TLS port. Script is shown here:
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
and it is working with SSL port in windows cygwin. Script is shown here:
import smtplib
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
回答4:
For me, it was because the network had dissallowed opening of sockets, try a different network if possible
来源:https://stackoverflow.com/questions/28328222/smtplib-of-python-not-working