I have a simple Python Program that is unable to send an email. It fails during the login process. I have tried using my GMAIL account, a new Yahoo Account and a new Hotmail account. I am confident I have the correct smtp server address and port number.
Here is the code:
import smtplib
from email.mime.text import MIMEText
# Email details
sender_email = "JTest13579@yahoo.com"
receiver_email = "wjacob@optonline.net"
password = "***************" # Password has been redacted for this Python Help Post
# Create the email content
message = MIMEText("This is a test email sent using Python and Yahoo Mail with SSL.")
message["Subject"] = "Test Email"
message["From"] = sender_email
message["To"] = receiver_email
# Connect to Yahoo Mail's SMTP server using SSL
with smtplib.SMTP_SSL("smtp.mail.yahoo.com", 465) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message.as_string())
print("Email sent successfully!")
The following is the error I received after trying to execute: the server.login line
Exception has occurred: SMTPServerDisconnected
Connection unexpectedly closed
File "C:\Users\Walter\Sending An Email From Python App.py", line 18, in <module>
server.login(sender_email, password)
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
Can anyone provide me with some guidance on how to resolve this issue?
Can you send emails from those providers, using any smtp client like Thunderbird? This would make sure those free email providers support smtp. Otherwise there are other providers that do, but they have a few more hoops to jump through to prove you’re not sending spam and eggs.
Thank you James. I successfully installed Thunderbird and was able to send an email from my Yahoo Account but only when the SMTP server settings used TLS/SSL and OAuth2 authentication. When I configured Thunderbird for password authentication the mail client failed at the login step - similar to my Python Application. I think this may be my problem. I am unsure how to get my Python application to use OAuth2. I went on Yahoo’s Developer site and they stated that I must register my client application to get a Client ID and Client Secret. Then there are several more steps to complete the OAuth2 process. This has become very complicated for me. I would prefer to find a simple mail server that I can login using a username and password in order to send an email. This is a prototype application for learning, and OAuth2 authentication seems to make it much more complicated at this point of my development and learning. Any suggestions?
Hi James, I am awaiting a response from MailGun’s support team. In the meantime, I believe the Thunderbird client is using OAuth2 authentication (and I imagine Yahoo and GMail do as well). Do you know of any sample Python code that implements OAuth2 authentication? I read where I will need to register my client, which I can do but it asks for the Domain website - which I don’t have. Yahoo and Gmail do not offer customer support for these types of questions from any of my search attempts. I am surprised it is so complicated to a novice python developer to simply send a text email. I do not have any Unix or Windows servers to setup a mail server. I was hoping to use something simple and free (no bells and whistles) on the internet. Any advice you can provide would be very much appreciated.
You may be interested in this library: GitHub - simonrob/email-oauth2-proxy: An IMAP/POP/SMTP proxy that transparently adds OAuth 2.0 authentication for email clients that don't support this method. . It creates a proxy that wraps the oauth2 process so that you can write “normal” Python code to log in to the server and do stuff. It requires a couple additional steps to get the oauth2 jumpstarted (like it gives you a URL to open in a browser and then paste a resulting key into the command line to set up the proxy). The documentation also gently suggests you copy the oauth2 client ID from an existing app like Thunderbird to avoid having to register your own. In theory this is bad, but if your program is small and not doing anything too large-scale it’s probably ok. The docs give a pretty good outline of how to proceed although it can take some tinkering to get the settings right.
I have no association with this library but I came across it and have used it to interact with Hotmail accounts. I haven’t used it to send mail though so not sure how it will work for that, but the docs claim it can handle SMTP as well as IMAP.