1. 程式人生 > 實用技巧 >python3 傳送郵件(文字)

python3 傳送郵件(文字)

code

#!/usr/bin/python3
import smtplib

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

# 第三方 SMTP 服務
mail_host="smtp.mxhichina.com" #設定伺服器
mail_user="[email protected]" #使用者名稱
mail_pass="12332121" #口令
sender = '[email protected]'
receivers = ['[email protected]'] # 接收郵件,可設定為你的QQ郵箱或者其他郵箱

message 
= MIMEText('<html><body><h1>Hello</h1>' + '<p>檢視 <a href="http://www.python.org">測試報告</a>...</p>' + '</body></html>', 'html', 'utf-8') message['From'] = Header("Automation") message['To'] = Header("all") subject = 'Api automation test report' 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(mail_user, receivers, message.as_string()) print ("郵件傳送成功") except smtplib.SMTPException as e: print(e) print (
"Error: 無法傳送郵件")