Search code examples
pythonpython-3.xemailsmtpsmtplib

"Connection Unexpectedly Closed" SMTP Gmail Python 3


Wondering whether anyone has any ideas on this?

When I run the following script it always creates the handshake with the SMTP and then cancels mid-process with the error "SMTPServerDisconnected: Connection unexpectedly closed"?

##Email addresses for sender and recpient are correct when in use.

import smtplib

smtpObj = smtplib.SMTP('smtp.gmail.com', 465)
smtpObj.ehlo()
smtpObj.login('###########@gmail.com', '#Password')
smtpObj.sendmail('#########@gmail.com', '#########@gmail.com', 'Subject: 
Test.')

smtpObj.quit()
##Connection end

Any ideas or thoughts would be much appreciated, thanks in advance! :)


Solution

  • Thanks for the suggestion, had a quick mess about afterwards and found that Gmail has a security feature that prevents the Anaconda package (Spyder) from connecting. To prevent conflict "Less Secure Apps" needs to be enabled for those with future problems! :)