Tag Archives: [773]smtplib.SMTPServerDisconnected: Connection unexpectedly closed

[773]smtplib.SMTPServerDisconnected: Connection unexpectedly closed

 

Output:
D:\Python\python3.exe "D:/PyCharm files/face/raspberry/smtp.py"
Traceback (most recent call last):
 File "D:/PyCharm files/face/raspberry/smtp.py", line 43, in <module>
  smtp.login( username, password )                #Login Server
 File "D:\Python\lib\smtplib.py", line 721, in login
  initial_response_ok=initial_response_ok)
 File "D:\Python\lib\smtplib.py", line 631, in auth
  (code, resp) = self.docmd("AUTH", mechanism + " " + response)
 File "D:\Python\lib\smtplib.py", line 421, in docmd
  return self.getreply()
 File "D:\Python\lib\smtplib.py", line 394, in getreply
  raise SMTPServerDisconnected("Connection unexpectedly closed")

smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Solution:

Add two lines of code in front of SMTP. Login (user name, password) to send the mail successfully. The code added is as follows:

smtp.ehlo()
smtp.starttls()

The above solution can be solved in win server, but not in Linux server

Secure mail needs to be sent through SSL

server = smtplib.SMTP()
server.connect(email_host,25)

Exception thrown:

smtplib.SMTPServerDisconnected: Connection unexpectedly closed

QQ mailbox supports secure e-mail and needs to send e-mail through SSL: when using the standard 25 port to connect to the SMTP server, it uses plaintext transmission, and the whole process of sending e-mail may be eavesdropped. To send e-mail more safely, you can encrypt the SMTP session, which is to create a SSL secure connection first, and then use the SMTP protocol to send e-mail

Modification code:

server = smtplib.SMTP_SSL()
server.connect(email_host,465)# Enable SSL messaging, port is usually 465