OSError: [Errno 65] No route to host. Python 3.5.3 - python

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

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

Related

Trying to send email using python and getting below timeout error [duplicate]

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
Use smtplib.SMTP_SSL() for a TLS connection.
There is nothing wrong with all commands below; my issue is indeed caused by IP restrictions. Thanks for all your answers.
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()

Getting "Network is unreachable" for python smtplib

I've tried to set up email confirmation for django app, and after a few failure steps I tried it with simple python module using smtplib (going through Corey's tutorial) but after the first part of tutorial when he got the email I got this error after about 30 seconds of waiting:
Traceback (most recent call last):
File "main.py", line 10, in <module>
with smtplib.SMTP('smtp.gmail.com', port) as smtp:
File "/usr/lib/python3.8/smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.8/smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.8/smtplib.py", line 308, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/usr/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
OSError: [Errno 101] Network is unreachable
This is the code
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(username, password)
subject = 'Test'
body = 'Testing smtp library'
msg = f'Subject: {subject}\n\n{body}'
smtp.sendmail(username, username, msg)
I'm using 2-factor authentication and app password, and I've checked the 16 digit password, However I don't know other ways to check if I'm able to sign in with these credentials.
The problem was that I was using a VPN (I don't know why because I'm logged in from VPN's location), However I got another problem and couldn't log in after that and got another error
Traceback (most recent call last):
File "main.py", line 12, in <module>
smtp.login(username, username)
File "/usr/lib/python3.8/smtplib.py", line 734, in login
raise last_exception
File "/usr/lib/python3.8/smtplib.py", line 723, in login
(code, resp) = self.auth(
File "/usr/lib/python3.8/smtplib.py", line 646, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials k18sm34466285edx.18 - gsmtp')
So I just tried another option. I used yagmail and it's just fine with the same credentials that I was using. So I guess the problem was that smtplib wasn't accepted from gmail.

smtplib could not connect to socket in python with WinError 10013 on Win 7

I am trying to send email using Python's smtplib library and getting error on a Win 7 VM. I have tried it on different machines/VM's and it has worked so far, but not sure why it is not working now on this particular VM. This is the code in send_email.py-
import smtplib
smtp_host='smtpmailserver.mycompany.com'
s = smtplib.SMTP(host=smtp_host)
Stacktrace-
File "C:\send_email.py", line 77, in send
s = smtplib.SMTP(host=smtp_host)
File "C:\python3.7\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\python3.7\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\python3.7\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "C:\python3.7\lib\socket.py", line 727, in create_connection
raise err
File "C:\python3.7\lib\socket.py", line 716, in create_connection
sock.connect(sa)
OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions
Appreciate any help.
I was getting the same error and i changed the port and it worked for me
s = smtplib.SMTP(host=smtp_host, port 587)

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

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
Use smtplib.SMTP_SSL() for a TLS connection.
There is nothing wrong with all commands below; my issue is indeed caused by IP restrictions. Thanks for all your answers.
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()

I'm trying to send email through 'localhost' in Python but it keeps giving me an "Error 61"

I'm running a Mac OS X and used postfix to enable localhost.
This is the error message I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 61] Connection refused
Use netstat to verify that a SMTP server is bound to port 25 of an appropriate interface in the first place.
Check the postfix log file, which might be at /var/log/mail or something similar, depending on how you installed postfix.

Categories

Resources