3

I'm trying to connect to gmail through SMTP, but python is not connecting to the server: smtp.gmail.com. I don't think there is something wrong with my code, but here it is:

import smtplib
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)

smtpObj.ehlo()
smtpObj.starttls()

smtpObj.login('sending_Email@gmail.com', 'password')
smtpObj.sendmail('sending_Email@gmail.com', 'receiving_Email@gmail.com')
smptObj.quit()

I think there is some settings I have to enable, but nothing is working. I am using a mac running OS X Yosemite 10.10.5. Thanks for your help in advance.

This is the error message I am getting.

Traceback (most recent call last):
File "/Users/jacobgreen/Documents/Comp Programming/Email Hack.py", line 3, in <module>
    smtpObj = smtplib.SMTP("smtp.gmail.com", 587)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/smtplib.py", line 335, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/smtplib.py", line 306, in _get_socket
    self.source_address)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 712, in create_connection
    raise err
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 703, in create_connection
    sock.connect(sa)
OSError: [Errno 65] No route to host
Jacob Green
  • 61
  • 1
  • 10
  • See [here](http://stackoverflow.com/a/27515833/4041795) – Montmons Mar 28 '17 at 16:29
  • @SB87 I updated my privacy settings, but my code is still getting caught at the first line and I get like 20 lines of error that pretty much just says that it has "No route to host" – Jacob Green Mar 28 '17 at 17:41
  • Did you study all the comments posted on the linked question? Multiple answers + their comments describe your.problem, your code looks very similar to code that the comments clearly mention as outdated. – Montmons Mar 28 '17 at 18:01
  • Also, see [here](https://ubuntuforums.org/showthread.php?t=1985418) for the `no route to host` problem. I didn't read all the answers but it is marked as solved. It also mentions gmail accounts. PS: If you cannot find the answer, please post the complete error-message you are getting. – Montmons Mar 28 '17 at 18:05
  • @SB87 thanks for your help, but it had something to do with the wifi I was on because I just tried it from my home wifi, and its now working. – Jacob Green Mar 29 '17 at 01:17
  • Great, could you post that as an answer and mark it as solved? Thnx! – Montmons Mar 29 '17 at 07:03

1 Answers1

2

If this is happening to you, try connecting to a different wifi network and attempt to run the code again. If I find a way to edit the setting on your computer needed to make the code work. I'll post it here

Jacob Green
  • 61
  • 1
  • 10