天天看點

使用smtplib發送郵件,提示smtplib.SMTPServerDisconnected: Connection unexpectedly closed

import smtplib
from email.mime.text import MIMEText
from email.header import Header


smtp_server='smtp.qq.com' # 要發送的伺服器
port=465 # 伺服器端口号
sender = ''  # 發送郵箱的賬号
receivers = ['']  # 接收郵件,可設定為你的QQ郵箱或者其他郵箱

mail_msg ='<h1>先測試一下</h1>'

message = MIMEText(mail_msg, 'html', 'utf-8')
message['From'] = Header("測試", 'utf-8')
message['To'] =  Header("測試", 'utf-8')

subject = 'Python SMTP 郵件測試' # 發送的郵箱主題
message['Subject'] = Header(subject, 'utf-8')


try:
    smtpObj = smtplib.SMTP(smtp_server,port)
    smtpObj.set_debuglevel(1)
    smtpObj.login(sender, sender_password)
    smtpObj.sendmail(sender, receivers, message.as_string())
    print("郵件發送成功")
    smtpObj.quit()
except smtplib.SMTPException as e:
    print("Error: 無法發送郵件{}".format(e))
           

上面有一個坑,因為使用的是QQ郵箱,一般需要先開啟 QQ郵箱設定裡的各種服務。比如使用pop.qq.com收件,用smtp.qq.com發件,根據需要開啟。

使用smtplib發送郵件,提示smtplib.SMTPServerDisconnected: Connection unexpectedly closed

開啟之後,會看到溫馨提示這裡有個授權碼,一堆七七八八的操作之後得到(因為是重要資訊圖檔處理了)授權碼,那麼我們需要給發送的郵箱授權。

使用smtplib發送郵件,提示smtplib.SMTPServerDisconnected: Connection unexpectedly closed

這裡的sender_password就是sender的授權碼

import smtplib
from email.mime.text import MIMEText
from email.header import Header


smtp_server='smtp.qq.com' # 要發送的伺服器
port=465 # 伺服器端口号
sender = ''  # 發送郵箱的賬号
sender_password=''  # 發送郵箱的授權碼
receivers = ['']  # 接收郵件,可設定為你的QQ郵箱或者其他郵箱

mail_msg ='<h1>先測試一下</h1>'

message = MIMEText(mail_msg, 'html', 'utf-8')
message['From'] = Header("測試", 'utf-8')
message['To'] =  Header("測試", 'utf-8')

subject = 'Python SMTP 郵件測試' # 發送的郵箱主題
message['Subject'] = Header(subject, 'utf-8')


try:
    smtpObj = smtplib.SMTP(smtp_server,port)
    smtpObj.set_debuglevel(1)
    smtpObj.login(sender, sender_password)
    smtpObj.sendmail(sender, receivers, message.as_string())
    print("郵件發送成功")
    smtpObj.quit()
except smtplib.SMTPException as e:
    print("Error: 無法發送郵件{}".format(e))
           

這時候我以為會發送成功,結果報錯了:

使用smtplib發送郵件,提示smtplib.SMTPServerDisconnected: Connection unexpectedly closed

資訊提示是連接配接關閉,然後檢查一下代碼,發現需要将smtplib.SMTP()改成smtplib.SMTP_SSL(),smtplib.SMTP()是使用明文傳輸,對郵件内容不加密,而smtplib.SMTP_SSL()是使用ssl加密方式,通信過程加密,郵件資料安全。

import smtplib
from email.mime.text import MIMEText
from email.header import Header


smtp_server='smtp.qq.com' # 要發送的伺服器
port=465 # 伺服器端口号
sender = ''  # 發送郵箱的賬号
sender_password=''  # 發送郵箱的授權碼
receivers = ['']  # 接收郵件,可設定為你的QQ郵箱或者其他郵箱

mail_msg ='<h1>先測試一下</h1>'

message = MIMEText(mail_msg, 'html', 'utf-8')
message['From'] = Header("測試", 'utf-8')
message['To'] =  Header("測試", 'utf-8')

subject = 'Python SMTP 郵件測試' # 發送的郵箱主題
message['Subject'] = Header(subject, 'utf-8')


try:
    smtpObj = smtplib.SMTP_SSL(smtp_server,port)  #将smtplib.SMTP()改成smtplib.SMTP_SSL()
    smtpObj.set_debuglevel(1)
    smtpObj.login(sender, sender_password)
    smtpObj.sendmail(sender, receivers, message.as_string())
    print("郵件發送成功")
    smtpObj.quit()
except smtplib.SMTPException as e:
    print("Error: 無法發送郵件{}".format(e))
           

于是終于發送成功。具體使用還請參考官方檔案。

https://docs.python.org/zh-cn/3.7/library/smtplib.html?highlight=email#smtplib.SMTP.quit

使用smtplib發送郵件,提示smtplib.SMTPServerDisconnected: Connection unexpectedly closed