Skip to content
Advertisement

Sending email with smtplib library with Python

Sending email with smtplib library with the below Python program, however, I get a SMTPServerDisconnected: Connection unexpectedly closed error.

JavaScript

Full error code below:

JavaScript

When trying out port 587 for Google SMTP servers I get an authentication error, so I imagine 465 is the correct TLS port. I have also seen another post about adding the ‘From’ and ‘To’ headers and therefore added in my own adaptation. I have also enabled POP3 and IMAP in both Gmail accounts.

Appreciate your help in advance, recently started Python!

Advertisement

Answer

This is a different way to send emails with Python in SMTPLIB.

JavaScript

Be careful to not fake you sent the email from an account you don’t own, if you do, it might cause some unwanted effects. Make sure that the emails you are typing in is valid and your own. Also, you might want to create a send email google account to send your mail to other people, because you will need to loosen security on that new email. First, create that gmail account and go to your circle avatar. Go to manage account > security > and toggle less secure apps to on. Don’t use this email for everything, only use it to send emails through python, because it’s safer.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement