smtplib.SMTPServerDisconnected:接続が予期せず閉じられたソリューション



Smtplib Smtpserverdisconnected



はじめに:Pythonでメールを送信する方法を学習する場合は、常にsmtplib.SMTPServerDisconnectedを送信するようにTencent Enterprise Mailboxを構成してください:接続が予期せず閉じられたエラー。オンラインメソッドを照会した後、次のソリューションを要約します。

1.エラーレポートの例:



2.解決策:解決策:



Change smtplib.SMTP() to smtplib.SMTP_SSL(mail_host,465).

3.ソースコードは次のように添付されています。

import smtplib from email.mime.text import MIMEText from email.header import Header sender ='××××@zhiqi.cn' # sender receivers = ['××××@zhiqi.com'] # recipients # Third-party SMTP service mail_host='smtp.exmail.qq.com' # Set the sending server mail_user ='××××@zhiqi.cn' # login email name mail_pass ='××××××' # Password (authorization code) # Three parameters: the first is the text content, the second plain sets the text format, and the third utf-8 sets the encoding message = MIMEText('python mail sending test','plain','utf-8') # send mail body message['From'] = Header('Star test','utf-8') # sender message['To'] = Header('test user','utf-8') # receiver subject ='Python SMTP mail test' # Send mail subject message['Subject'] = Header(subject,'utf-8') try: smtpObj = smtplib.SMTP_SSL(mail_host,465) # The port number of the sending server smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(sender,receivers,message.as_string()) print('Mail sent successfully') except smtplib.SMTPException: print('Mail sending failed')