日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

python3:利用SMTP协议发送QQ邮件+附件

發布時間:2023/11/27 生活经验 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3:利用SMTP协议发送QQ邮件+附件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載請表明出處:https://www.cnblogs.com/shapeL/p/9115887.html

1.發送QQ郵件,首先必須知道QQ郵箱的SMTP服務器

http://service.mail.qq.com/cgi-bin/help?id=28&no=167&subtype=1

2.發送郵件之前,必須開啟qq郵箱的smtp服務

設置路徑:郵箱設置--賬戶--開啟截圖服務--保存更改

3.代碼拋出異常分析

(1)郵箱密碼傳入值為日常登錄密碼,報錯

global send_user
global email_host
global passwordpassword = 'xxx92'
email_host = "smtp.qq.com"
send_user = "11xxx@qq.com"

拋出異常:

smtplib.SMTPAuthenticationError:(535, b'Error: \xc7\xeb\xca\xb9\xd3\xc3\xca\xda\xc8\xa8\xc2\xeb\xb5\xc7\xc2\xbc\xa1\xa3\xcf\xea\xc7\xe9\xc7\xeb\xbf\xb4: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256')

打開拋出異常中的鏈接:是關于授權碼的介紹,根據介紹,登錄時應該使用授權碼作為登錄密碼,該處的授權碼是開啟服務時收到的16位授權碼

http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256

修改代碼:

password = "lunkbrgwqxhfjgxx"(對應的16位授權碼)

(2)安全郵件,需要通過SSL發送

server = smtplib.SMTP()
server.connect(email_host,25)

拋出異常:

smtplib.SMTPServerDisconnected: Connection unexpectedly closed

QQ郵箱是支持安全郵件的,需要通過SSL發送的郵件:使用標準的25端口連接SMTP服務器時,使用的是明文傳輸,發送郵件的整個過程可能會被竊聽。要更安全地發送郵件,可以加密SMTP會話,實際上就是先創建SSL安全連接,然后再使用SMTP協議發送郵件

修改代碼:

server = smtplib.SMTP_SSL()
server.connect(email_host,465)# 啟用SSL發信, 端口一般是465

4.附上完整代碼

#coding:utf-8
import smtplib
from email.mime.text import MIMETextclass SendEmail:global send_userglobal email_hostglobal passwordpassword = "lunkbrgwqxhfjgxx"email_host = "smtp.qq.com"send_user = "11xx@qq.com"def send_mail(self,user_list,sub,content):user = "shape" + "<" + send_user + ">"message = MIMEText(content,_subtype='plain',_charset='utf-8')message['Subject'] = submessage['From'] = usermessage['To'] = ";".join(user_list)server = smtplib.SMTP_SSL()server.connect(email_host,465)server.login(send_user,password)server.sendmail(user,user_list,message.as_string())server.close()if __name__ == '__main__':send = SendEmail()user_list = ['11xx@qq.com']sub = "測試郵件"content = "ceshi看看"send.send_mail(user_list,sub,content)

(1)Python對SMTP支持有smtplib和email兩個模塊,email負責構造郵件,smtplib負責發送郵件

(2)構造MIMEText對象時,第一個參數是郵件正文;第二個參數是MIME的subtype,傳入'plain'表示純文本,最終的MIME就是'text/plain';最后一定要用utf-8編碼保證多語言兼容性

(3)發送的郵件需要添加頭部信息,頭部信息中包含發送者、接收者、郵件主題等信息:message['From']、message['To']、message['Subject']

(4)構造完要發送的郵件信息后,通過SMTP發出去:login()方法用來登錄SMTP服務器;sendmail()方法就是發郵件,由于可以一次發給多個人,所以傳入一個list;郵件正文是一個str,as_string()把MIMEText對象變成str

(5)SMTP.close() :關閉SMTP服務器連接

?

5、發送郵件帶附件

參考代碼:

?

#coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipartclass SendEmail:global send_userglobal email_hostglobal passwordpassword = "lunkbrgwqxhfjgxx"email_host = "smtp.qq.com"send_user = "xx@qq.com"def send_mail(self,user_list,sub,content):user = "shape" + "<" + send_user + ">"# 創建一個帶附件的實例message = MIMEMultipart()message['Subject'] = submessage['From'] = usermessage['To'] = ";".join(user_list)# 郵件正文內容message.attach(MIMEText(content, 'plain', 'utf-8'))# 構造附件(附件為txt格式的文本)att = MIMEText(open('../log/log.txt', 'rb').read(), 'base64', 'utf-8')att["Content-Type"] = 'application/octet-stream'att["Content-Disposition"] = 'attachment; filename="Log.txt"'message.attach(att)server = smtplib.SMTP_SSL()server.connect(email_host,465)# 啟用SSL發信, 端口一般是465# server.set_debuglevel(1)# 打印出和SMTP服務器交互的所有信息
        server.login(send_user,password)server.sendmail(user,user_list,message.as_string())server.close()def send_main(self,pass_list,fail_list,no_run_list):pass_num = len(pass_list)fail_num = len(fail_list)#未執行的用例no_run_num = len(no_run_list)count_num = pass_num + fail_num + no_run_num#成功率、失敗率'''用%對字符串進行格式化%d 格式化整數%f 格式化小數;想保留兩位小數,需要在f前面加上條件:%.2f;用%%來表示一個%如果你不太確定應該用什么,%s永遠起作用,它會把任何數據類型轉換為字符串 '''pass_result = "%.2f%%" % (pass_num/count_num*100)fail_result = "%.2f%%" % (fail_num/count_num*100)no_run_result = "%.2f%%" % (no_run_num/count_num*100)user_list = ['xx@qq.com']sub = "接口自動化測試報告"content = "總共執行接口個數%s個,通過個數%s個,失敗個數%s個,未執行個數%s個:通過率為%s,失敗率為%s,未執行率為%s" % (count_num,pass_num,fail_num,no_run_num,pass_result,fail_result,no_run_result)self.send_mail(user_list,sub,content)

?

舉例說明附件為TXT類型,其他類型的可參考:https://blog.csdn.net/u013250071/article/details/79037843

?

轉載于:https://www.cnblogs.com/shapeL/p/9115887.html

總結

以上是生活随笔為你收集整理的python3:利用SMTP协议发送QQ邮件+附件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。