Python—發送郵件

1、發送純文本郵件

import smtplib
from email.mime.text import MIMEText

subject = "標題"   # 郵件的主題
content = "測試"   # 郵件的內容
sender = "1548429568@qq.com"    # 發件人
password = "qnrwndesjxmmijce"   # 剛纔咱們在QQ郵箱裏設置的受權密碼
receiver = "3235403488@qq.com"  # 收件人

message = MIMEText(content, "plain", "utf-8")   # 內容,格式,編碼格式
message["From"] = sender        # 發送郵箱
message["To"] = receiver        # 接收郵箱
message["Subject"] = subject    # 標題
# message = '\n'.join(['From: {}'.format(sender), 'To: {}'.format(receiver), 'Subject: {}'.format(subject), '', content])

# smtp = smtplib.SMTP()                        # 普通的郵件發送形式
smtp = smtplib.SMTP_SSL("smtp.qq.com", 465)    # QQ郵箱的SMTP服務器(端口465或587)
smtp.set_debuglevel(1)                       # 用set_debuglevel(1)就能夠打印出和SMTP服務器交互的全部信息
smtp.login(sender, password)                   # 登陸SMTP服務器,輸入發送郵箱和密碼
smtp.sendmail(sender, receiver, message.as_string())
smtp.quit()
smtp.close()
相關文章
相關標籤/搜索