這篇文章給大家分享的是有關python如何發送qq郵件的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
python自帶了兩個模塊smtplib和email用于發送郵件。smtplib模塊主要負責發送郵件,它對smtp協議進行了簡單的封裝。email模塊主要負責郵件的構造。
email包下有三個模塊:MIMEText,MIMEImage,MIMEMultipart
import smtplib from email.header import Header from email.mime.text import MIMEText sender = '888888@qq.com' # 發送使用的郵箱 receivers = ['888888@qq.com'] # 收件人,可以是多個任意郵箱 message = MIMEText('這里是正文!', 'plain', 'utf-8') message['From'] = Header("發送者", 'utf-8') # 發送者 message['To'] = Header("接收者", 'utf-8') # 接收者 subject = '這里是主題!' message['Subject'] = Header(subject, 'utf-8') try: # qq郵箱服務器主機 # 常見其他郵箱對應服務器: # qq:smtp.qq.com 登陸密碼:系統分配授權碼 # 163:stmp.163.com 登陸密碼:個人設置授權碼 # 126:smtp.126.com 登陸密碼:個人設置授權碼 # gmail:smtp.gmail.com 登陸密碼:郵箱登錄密碼 smtp = smtplib.SMTP_SSL('smtp.qq.com') # 登陸qq郵箱,密碼需要使用的是授權碼 smtp.login(sender, 'abcdefghijklmn') smtp.sendmail(sender, receivers, message.as_string()) smtp.quit() print("郵件發送成功") except smtplib.SMTPException: print("Error: 無法發送郵件")
html = """ <html> <body> <h3> HTML </h3> <div style='font-weight:bold'> 格式郵件 </div> </body> </html> """ message = MIMEText(html,'html', 'utf-8')
html = """ <html> <body> <h3> HTML </h3> <div style='font-weight:bold'> 格式郵件帶圖片 </div> <img src="cid:imageTest"> </body> </html> """ message = MIMEMultipart('related') messageAlter = MIMEMultipart('alternative') message.attach(messageAlter) messageAlter.attach(MIMEText(html, 'html', 'utf-8')) # 指定圖片為當前目錄 fp = open('test.png', 'rb') messageImage = MIMEImage(fp.read()) fp.close() # 定義圖片ID,和圖片中的ID對應 messageImage.add_header('Content-ID', '<imageTest>') message.attach(messageImage)
from email.mime.multipart import MIMEMultipart message = MIMEMultipart() message.attach(MIMEText('這里一封帶附件的郵件!', 'plain', 'utf-8')) # 添加附件 # 其他格式如png,rar,doc,xls等文件同理。 attach = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8') attach["Content-Type"] = 'application/octet-stream' attach["Content-Disposition"] = 'attachment; filename="test.txt"' message.attach(attach)
感謝各位的閱讀!關于“python如何發送qq郵件”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。