日誌11月24日

sbqaqsjb發表於2020-11-24

根據昨天的基礎,我改進了一下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import smtplib
from email.mime.text import MIMEText
from email.header import Header
 
# 第三方 SMTP 服務
mail_host="smtp.XXX.com"  #設定伺服器
mail_user="XXXX"    #使用者名稱
mail_pass="XXXXXX"   #口令 
 
 
sender = '趙冠宇'
receivers = ['3110391687@qq.com']  # 接收郵件,可設定為你的QQ郵箱或者其他郵箱
 
message = MIMEText('Python 郵件傳送測試...', 'plain', '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() 
    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: 無法傳送郵件"

這是HTML版的:

import smtplib
from email.mime.text import MIMEText
from email.header import Header
 
sender = 'from@runoob.com'
receivers = ['429240967@qq.com']  # 接收郵件,可設定為你的QQ郵箱或者其他郵箱
 
mail_msg = """
<p>Python 郵件傳送測試...</p>
<p><a href="https://mail.qq.com/">這是一個連結</a></p>
"""
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('localhost')
    smtpObj.sendmail(sender, receivers, message.as_string())
    print "郵件傳送成功"
except smtplib.SMTPException:
    print "Error: 無法傳送郵件"