import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 第三方 SMTP 服務
mail_host = "smtp.163.com" # 設定服務器
mail_user = "[email protected]" # 發郵件的賬戶名
mail_pass = "******" # 授權碼
sender = '[email protected]'
receivers = ['[email protected]'] # 接收郵件,可設定為你的QQ郵箱或者其他郵箱
# 三個引數:第一個為文本內容,第二個設定格式,plain:文本,html:HTML格式,第三個 utf-8 設定編碼
message = MIMEText('本次郵件的內容', 'plain', 'utf-8')
message['From'] = Header("[email protected]") # 郵件中的發件人
message['To'] = Header("[email protected]") # 郵件中的收件人
subject = '郵件主題'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 為 SMTP 埠號
smtpObj.login(mail_user, mail_pass)
# 發件人郵箱賬號、收件人郵箱賬號、發送郵件
smtpObj.sendmail(sender, receivers, message.as_string())
print("郵件發送成功")
except smtplib.SMTPException:
print("Error: 無法發送郵件")
SMTP 服務器

授權碼

參考
https://www.runoob.com/python/python-email.html
解決 554 DT:SPM 問題
https://www.jianshu.com/p/d487bd784ed0
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/197787.html
標籤:Python
