SMTP modules in python - 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

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

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

Is this the way of using smtplib

I am trying to send emails using python and smptplib but it shows some errors the following is my code:
import smtplib,ssl
port = 456
password = input("enter your password : ")
email_id = input("enter your email adress: ")
print("creating connection...")
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com",port,context=context) as server:
server.login(email_id,password)
Traceback (most recent call last):
File "/Users/rijilvarghese/Documents/email.py/newemail.py", line 9, in <module>
with smtplib.SMTP_SSL("smtp.gmail.com",port,context=context) as server:
File "/opt/anaconda3/lib/python3.7/smtplib.py", line 1031, in __init__
source_address)
File "/opt/anaconda3/lib/python3.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/opt/anaconda3/lib/python3.7/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/opt/anaconda3/lib/python3.7/smtplib.py", line 1037, in _get_socket
self.source_address)
File "/opt/anaconda3/lib/python3.7/socket.py", line 727, in create_connection
raise err
File "/opt/anaconda3/lib/python3.7/socket.py", line 716, in create_connection
sock.connect(sa)
OSError: [Errno 65] No route to host
this Are the errors,What should i do?

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