python smtplib ssh方式發送QQ郵件(其他商業郵箱通用)例子

#! /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爲收件人郵箱,內容和標題都可根據需求更改

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章