Vipul Vipul - 5 months ago 14
Python Question

smtplib of python not working

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

Answer

Use port 465. From Google docs on SMTP configuration;

enter image description here

import smtplib
s = smtplib.SMTP_SSL('smtp.gmail.com', 465)