0
port = 465  # For SSL
smtp_server = "smtp.mail.yahoo.com"
sender_email = "c.junction@yahoo.com"  # Enter your address
password = input("Type your password and press enter:")
receiver_email = "jawoneb660@jobsfeel.com"  # Enter receiver address
Subject = "Hi there"
message = """Hello World!!!
This message is sent from Python."""
context = ssl.create_default_context()

with smtplib.SMTP_SSL(smtp_server, port, context=context) as smtp:
     smtp.login(sender_email, password)
     smtp.sendmail(sender_email, receiver_email, Subject, message)


I tried checking if there is any auth failure.Email could not be sent. I got following error msg.

raise SMTPServerDisconnected("Connection unexpectedly closed") smtplib.SMTPServerDisconnected: Connection unexpectedly closed`

1 Answers1

-1
import smtplib ##Import needed modules
import ssl

port = 465  # For SSL
smtp_server = "smtp.mail.yahoo.com"
sender_email = "c.junction@yahoo.com"  # Enter your address
password = input("Type your password and press enter:")
receiver_email = "jawoneb660@jobsfeel.com"  # Receiver address
Subject = "Hi there"

message = """Hello World!!!
This message is sent from Python."""

context = ssl.create_default_context()
 
try:
    print("Connecting to server...")
    yahoo_server = smtplib.SMTP(smtp_server, port)
    yahoo_server.starttls(context=context)
    yahoo_server.login(sender_email, password)

    print("Connected to server!")

    print(f"Sending email to - {receiver_email}")
    yahoo_server.sendmail(sender_email, receiver_email, Subject, message)
    print("Email successfully sent to - {receiver_email}")

except Exception as e:
    print(e)

finally:
    yahoo_server.quit

I added a couple of modules that I didn't see. But you need those in order to use the ssl methods as well as the smtplib.

I also added a couple of print statements to help with seeing where in the process this might be getting hung up. You also want to close the connection as best practice.

lastly I added a variable to use with the steps of logging into the smtp server. I did this for Gmail recently so I don't know if this will work offhand, but at the very least the print statements and additional variables should hopefully help with that as well.

  • Thanks buddy for the reply & inputs. It did help me to send an email via gmail however code failed for yahoomail. – Gaarrud Feb 22 '23 at 09:43