1.發送QQ郵件,首先必須知道QQ郵箱的SMTP伺服器
http://service.mail.qq.com/cgi-bin/help?id=28&no=167&subtype=1

2.發送郵件之前,必須開啟qq郵箱的smtp服務
設定路徑:郵箱設定--賬戶--開啟截圖服務--儲存更改
3.代碼抛出異常分析
(1)郵箱密碼傳入值為日常登入密碼,報錯
globalsend_userglobalemail_hostglobalpassword
password= 'xxx92'email_host= "smtp.qq.com"send_user= "[email protected]"
抛出異常:
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
importsmtplibfrom email.mime.text importMIMETextclassSendEmail:globalsend_userglobalemail_hostglobalpassword
password= "lunkbrgwqxhfjgxx"email_host= "smtp.qq.com"send_user= "[email protected]"
defsend_mail(self,user_list,sub,content):
user= "shape" + "<" + send_user + ">"message= MIMEText(content,_subtype='plain',_charset='utf-8')
message['Subject'] =sub
message['From'] =user
message['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= ['[email protected]']
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
importsmtplibfrom email.mime.text importMIMETextfrom email.mime.multipart importMIMEMultipartclassSendEmail:globalsend_userglobalemail_hostglobalpassword
password= "lunkbrgwqxhfjgxx"email_host= "smtp.qq.com"send_user= "[email protected]"
defsend_mail(self,user_list,sub,content):
user= "shape" + "<" + send_user + ">"
#建立一個帶附件的執行個體
message =MIMEMultipart()
message['Subject'] =sub
message['From'] =user
message['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()defsend_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= ['[email protected]']
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)