Getting "Network is unreachable" for python smtplib - python

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.

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()

SMTP modules in python

I tried to write an email using python's smtplib module. Here is my code as well as the error displayed below. Thank you!
import smtplib
my_email = 'xxxxxxxxxxx#hotmail.com'
password = 'xxxxxxxxxx'
# I hid my email and password here
with smtplib.SMTP('outlook.office365.com') as connection:
connection.starttls()
connection.login(user=my_email, password=password)
connection.sendmail(from_addr=my_email, to_addrs=my_email, msg='helloworld')
/usr/local/bin/python3 "/Users/guanhongjiang/PycharmProjects/pythonProject/Birthday Wisher (Day 32) start/main.py"
Traceback (most recent call last):
File "/Users/guanhongjiang/PycharmProjects/pythonProject/Birthday Wisher (Day 32) start/main.py", line 6, in <module>
with smtplib.SMTP('outlook.office365.com') as connection:
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socket.py", line 845, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/socket.py", line 833, in create_connection
sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out

Sending corporate email with Python: TimeoutError

I'm trying to send an email using a corporate email address with smtplib in Python 3.7.
Here's my code:
import smtplib
from email.mime.text import MIMEText
from_addr='myEmail#corporate.com'
to_addr='myEmail#corporate.com'
server = smtplib.SMTP('corporateServer', 'port')
server.starttls()
msg = MIMEText('send my python', 'plain', 'utf-8')
server.sendmail(from_addr, to_addr, msg.as_string())
server.quit()
Error:
Traceback (most recent call last): File "script.py", line 51, in
<module>
server = smtplib.SMTP(smtpServer, 25) File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py",
line 251, in __init__
(code, msg) = self.connect(host, port) File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py",
line 336, in connect
self.sock = self._get_socket(host, port, self.timeout) File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py",
line 307, in _get_socket
self.source_address) File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 727, in create_connection
raise err File "/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 716, in create_connection
sock.connect(sa) TimeoutError: [Errno 60] Operation timed out

OSError: [Errno 65] No route to host. Python 3.5.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
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

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()

Categories

Resources