pythonデバッグpythonメールにsmtplibが表示されます.SMTPServer Disconnected:Connection unexpectedly closed問題の解決方法
2185 ワード
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
#
receiver = '[email protected]'
sender = '[email protected]'
# SMTP ( sender SMTP )
smtpserver = 'smtp.qq.com'
# ( )
username = '[email protected]'
password = 'moxxxxxxxxxxxfcd' #([email protected] )
mail_title = ' !'
mail_body = ' '
#
message = MIMEText( mail_body, 'plain', 'utf-8' ) #
# (plain mail_body , text, mail_body , )
message ['From'] = sender #
message['To'] = receiver #
message['Subject'] = Header( mail_title, 'utf-8' ) #
smtp = smtplib.SMTP() #
smtp.connect( smtpserver ) #
smtp.login( username, password ) #
smtp.sendmail( sender, receiver, message.as_string() ) #
smtp.quit()
実行結果:
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 smtp.login(username,password)#ログインサーバFile"D:Pythonlibsmtplib.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
ソリューション:
smtp.login(username,password)の前に2行のコードを追加すると、メールの正常な送信が実現します.追加されたコードは次のとおりです.
smtp.ehlo()
smtp.starttls()