1. 程式人生 > >python怎麼用qq郵箱傳送郵件

python怎麼用qq郵箱傳送郵件

使用SSL的通用配置如下:

接收郵件伺服器:pop.qq.com ,使用SSL,埠 995

傳送郵件伺服器: smtp.qq.com,使用SSL,埠 465或 587

賬戶名:QQ郵箱賬戶名(不用加“@qq.com”)

步驟一: 去設定-->賬戶-》找到POP3/IMAP  開啟POP3/SMTP服務

程式碼如下:

# coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 第三方 SMTP 服務
mail_host = "smtp.qq.com"  # 設定伺服器
mail_user = "[email protected]"  # 使用者名稱
mail_pass = "pgwhjrzdupxxxxb"  # 口令,QQ郵箱是輸入授權碼,在qq郵箱設定 裡用驗證過的手機發送簡訊獲得,不含空格

sender = '[email protected]'
receivers = ['[email protected]']  # 接收郵件,可設定為你的QQ郵箱或者其他郵箱

message = MIMEText('python傳送郵件', 'plain', 'utf-8')
message['From'] = Header("[email protected]", 'utf-8')
message['To'] = Header("[email protected]", 'utf-8')

subject = '使用python傳送郵件的內容'
message['Subject'] = Header(subject, 'utf-8')

try:
    smtpObj = smtplib.SMTP_SSL(mail_host, 465)
    smtpObj.login(mail_user, mail_pass)
    smtpObj.sendmail(sender, receivers, message.as_string())
    smtpObj.quit()
    print u"郵件傳送成功"
except smtplib.SMTPException, e:
    print e

結果如下圖: