Python Email Server not responding - python

Input
import smtplib
conn = smtplib.SMTP('imap.gmail.com',587)
conn.ehlo()
conn.starttls()
conn.login('loginmail#gmail.com', 'password')
conn.sendmail('sender1#gmail.com','sender2#gmail.com','Subject: What you like? \n\n Reply Reply Reply')
conn.quit()
Output and Error
Traceback (most recent call last):
File "E:\python\openCV\em.py", line 4, in <module>
conn = smtplib.SMTP('imap.gmail.com',587)
File "C:\Users\loges\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 251, in _init_
(code, msg) = self.connect(host, port)
File "C:\Users\loges\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\loges\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "C:\Users\loges\AppData\Local\Programs\Python\Python36\lib\socket.py", line 724, in create_connection
raise err
File "C:\Users\loges\AppData\Local\Programs\Python\Python36\lib\socket.py", line 713, in create_connection
sock.connect(sa)
TimeoautError: [WinError 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
Please help me to solve this issue. It looks like server issue. I have included the login mail and password, 2 sender emails and a subject. I want to send a zip file. The program is showing error and I have provided the error above

You are connecting to the wrong server. Should be:
conn = smtplib.SMTP('smtp.gmail.com', 587)

Related

Script to Send Automated Emails via Outlook

I am attempting to set up a script that will fire off a series of emails automatically.
Below I have the code I am using to test this process.
import smtplib, ssl
username = 'me#company.com' # input('Enter username: ')
password = 'mypassword' # input('Enter password: ')
port = 443
smtp_server = "outlook.office365.com"
sender_email = username
receiver_email = "me#company.com"
message = "Hi there! This message is sent from Python."
context = ssl.create_default_context()
# s = smtplib.SMTP_SSL('outlook.office365.com')
# s.set_debuglevel(1)
with smtplib.SMTP(smtp_server, port) as server:
server.starttls(context=context)
server.login(username, password)
server.sendmail(sender_email, receiver_email, message)
When I run it, I get the following error:
Traceback (most recent call last):
File "/Users/gx8k/repos/namespace_auditer/./send_mail.py", line 15, in <module>
s = smtplib.SMTP_SSL('outlook.office365.com')
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1050, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1056, in _get_socket
new_socket = super()._get_socket(host, port, timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 844, in create_connection
raise err
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 832, in create_connection
sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out
I want to send the email from Outlook specifically.
Checking the 'Accounts' settings I see my server is running on port 443 and has a domain of https://outlook.office365.com/subdomain/Exchange.asmx
I'm only using the outlook.office365.com portion of it since that's all that is shown in most examples I see. However I have tried using the full domain as well as just https://outlook.office365.com/. Nothing seems to work.
I have been using the info found here to guide me.
Originally I was using the domain smtp.office365.com but changed it after finding outlook.office365.com in my accounts settings. It fails with the same error anyway.
Traceback (most recent call last):
File "/Users/gx8k/repos/namespace_auditer/./send_mail.py", line 15, in <module>
s = smtplib.SMTP_SSL('smtp.office365.com')
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1050, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1056, in _get_socket
new_socket = super()._get_socket(host, port, timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 844, in create_connection
raise err
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 832, in create_connection
sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out
I also tried with port 587 rather than 443. (443 is what I find in my outlook settings) - same error either way.
Update: I think the answer to my question is that mac's don't use SMTP...now I have to figure out how to do this in IMAP.
What protocol does Outlook for Mac use?
By default, Outlook is set to IMAP protocol. If you choose to set your account on IMAP, you may use the following settings then click Add Account.
Additional Update:
I ended up scratching this approach altogether. I ended up using an internal API service to send notifications out to all relevant parties.
For Office 365 accounts, the SMTP server name is smtp.office365.com, post 587, TLS on.

TimeoutError: [Errno 60] Operation timed out (starttls())

I was having previously an issue where I was getting ValueError: server_hostname cannot be an empty string or start with a leading dot. Checked this post in Stack Overflow and when I followed the instruction and run in the console:
import smtplib
smtplib.SMTP_SSL(host='gmail-smtp-in.l.google.com').connect(host='gmail-smtp-in.l.google.com', port=25)
but I am getting a time-out as per below error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 1034, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 1040, in _get_socket
new_socket = socket.create_connection((host, port), timeout,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
I get the issue only when I enable in my code server.starttls(). If this is commented out, it can send email fine.
Below is a snippet of my code:
server = smtplib.SMTP_SSL()
server.connect('gmail-smtp-in.l.google.com', 25) # mail server want to connect to.
server.set_debuglevel(True) # shows communication with the server in the console
server.ehlo()
server.starttls()
server.ehlo()
maybe you could try using python 3.9. I dont know what operating system youre using but if youre using mac you have to use pip3 instead of pip so python can understand it (mac has python 2 as default) Hope i could help :)

Connecting to SMTP server failing on Linux but not Windows

In a python script, I have the following:
with smtplib.SMTP_SSL(sender_server, 465, context=context) as server:
server.login(sender_email, sender_password)
server.sendmail(
sender_email, email, message.as_string()
)
On the Windows machine I wrote the script on, everything works as expected and the email is sent and delivered without issue. However, when I try to run the same code on my Linux VPS, a TimeoutError is thrown every time.
Traceback (most recent call last):
File "script.py", line 151, in <module>
with smtplib.SMTP_SSL(sender_server, 465, context=context) as server:
File "/usr/lib/python3.6/smtplib.py", line 1031, in __init__
source_address)
File "/usr/lib/python3.6/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.6/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.6/smtplib.py", line 1037, in _get_socket
self.source_address)
File "/usr/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/usr/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out
What could be causing this difference? My Google searches didn't bear much fruit, but maybe I'm just searching for the wrong terms.
TimeoutError: [Errno 110] Connection timed out
This means that access to the remote system is likely blocked somewhere on the way to the system. Given that you have this problem on a "Linux VPS" it is likely that the connection is blocked by the company hosting this VPS or that you need to configure your machine specifically to allow such access. Check with your specific hoster and its documentation.

How to send a email by using smtplib properly?

I wrote a code to send email using smtplib and gives me error that 'connection refused'
This is the error traceback
Traceback (most recent call last):
File "mail.py", line 20, in <module>
s = smtplib.SMTP('localhost')
File "/usr/lib/python3.6/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.6/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.6/smtplib.py", line 307, in _get_socket
self.source_address)
File "/usr/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/usr/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
python file
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg.set_content('Hello, This message is sent by using python
smtplib')
msg['Subject'] = 'Use of Smtp'
msg['From'] = 'sen#gmail.com'
msg['To'] = 'rec#gmail.com'
# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()
if you want to send mail to gmail you have to modify gmail development settings.
refer below for more info: https://realpython.com/python-send-email/

SOCKET ERROR: [Errno 111] Connection refused on Ubuntu

Traceback (most recent call last):
s = smtplib.SMTP('localhost')
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 111] Connection refused
Q: I am getting this error on my ubuntu machine. Question is why so?
The reason being when I execute the same code on Mac OS X 10.7 I don't see this error. And I did not do any special configuration on Mac for this to work.
Make sure that your system is running smtp server:
netstat -nlt | grep '\<25\>'
If your system is running the smtp server, the above command will show somehting like this:
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN

Categories