smtplib of python not working

蓝咒 提交于 2019-12-11 17:58:10

问题


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

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