#! /usr/bin/env python#coding=utf-8
import smtplib
from email.mime.text import MIMEText
msg_from='[email protected]' #發送方郵箱
passwd='bbbb' #填入發送方郵箱的授權碼
msg_to='[email protected]'
#收件人郵箱
subject="這個測試好玩嗎" #主題
content="我在測試用腳本發郵箱,我覺得蠻好玩的,要不要一起玩" #正文
msg = MIMEText(content)
msg['Subject'] = subject
msg['From'] = msg_from
msg['To'] = msg_to
try:
s = smtplib.SMTP_SSL("smtp.qq.com",465)#郵件服務器及端口號
s.login(msg_from, passwd)#登錄SMTP服務器
s.sendmail(msg_from, msg_to, msg.as_string())#發郵件 as_string()把MIMEText對象變成str
print ("發送成功")
except s.SMTPException:
print ("發送失敗")
finally:
s.quit()
其中密碼需要在qq郵箱設置->賬戶POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務->開啓POP3/SMTP服務 即可獲得一組密碼
aaaa爲發信人郵箱,bbbb爲上一句話中獲取的密碼,cccc爲收件人郵箱,內容和標題都可根據需求更改