歡迎您光臨本站 註冊首頁

Python基於QQ郵箱實現SSL發送

←手機掃碼閱讀     火星人 @ 2020-04-26 , reply:0

這篇文章主要介紹了Python基於QQ郵箱實現SSL發送,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

一、QQ郵箱SSL發送

獲取qq授權碼

ssl發送方式不是使用郵箱密碼,而是需要授權碼,具體步驟如下:

登錄發送人qq郵箱>>設置>>賬戶>>POP3/STMP服務開啟>>生成授權碼

驗證密保

複製16位授權碼

qq郵箱發送源碼

#!/usr/bin/python3 # encoding:utf-8 ''' Created on 2020-04-24 12:15 @author: Administrator ''' #coding:utf-8 import smtplib from email.mime.text import MIMEText # 引入smtplib和MIMEText from email.mime.multipart import MIMEMultipart #設置SMTP地址 host = 'smtp.qq.com' #設置發件服務器端口號,注意,這裡有SSL和非SSL兩種形式,qq SSL端口為465,非SSL為端口默認25 port = "465" #設置發件郵箱 sender = "357@qq.com" #設置發件郵箱的授權碼 ,qq郵箱ssl發送需要先開啟stmp並獲取密碼 pwd = 'sqmqweertyuiioplk' #16授權碼 #設置郵件接收人,發送給多人,隔開 receiver = 'yiwr@163.com,7894@qq.com' #設置郵件抄送人,發送給多人,隔開 cc = 'ywr198592@126.com' ''' 不帶附件發送郵件 #設置html格式的郵件 #body = '

這是一個python測試郵件

test

' #msg = MIMEText(body, 'html') # 設置正文為符合郵件格式的HTML內容 #發送普通格式郵件 msg = MIMEText('Python 普通格式,郵件發送測試...', 'plain', 'utf-8') ''' #需要發送附件的方法實例 msg = MIMEMultipart() #設置發送頭信息 msg.add_header('subject', '測試郵件') #設置郵件標題 msg.add_header('from', sender) # 設置發送人 msg.add_header('to', receiver) # 設置接收人 msg.add_header('Cc',cc) # 抄送人 #設置正文內容 msg.attach(MIMEText('Python 郵件發送測試...', 'plain', 'utf-8')) #設置附件1,D://cs.txt 文件 att1 = MIMEText(open('D://cs.txt', 'rb').read(), 'base64', 'utf-8') att1.add_header('Content-Type', 'application/octet-stream') # 這裡的filename可以任意寫,寫什麼名字,郵件中顯示附件的名字 att1.add_header('Content-Disposition', 'attachment', filename='cs.txt') msg.attach(att1) try: #注意!如果是使用非SSL端口,這裡就要改為SMTP smtpObj = smtplib.SMTP_SSL(host, port) #登陸郵箱 smtpObj.login(sender, pwd) #發送郵件,注意第二個參數是發送人抄送人地址 smtpObj.sendmail(sender, receiver.split(',') + cc.split(','), msg.as_string()) print ("發送成功") except smtplib.SMTPException as e: print ("發送失敗") print(e) finally: smtpObj.quit()

發送之後結果截圖

二、163郵箱非SSL發送

非ssl無需獲取授權碼,直接配置郵箱密碼即可

163郵箱發送源碼

#!/usr/bin/python3 #encoding:utf-8 ''' Created on 2020-04-24 12:15 @author: Administrator '''

#coding:utf-8 import smtplib from email.mime.text import MIMEText #引入smtplib和MIMEText from email.mime.multipart import MIMEMultipart

#設置SMTP地址 host = 'smtp.163.com'

#設置發件服務器端口號。注意,這裡有SSL和非SSL兩種形式,非SSL默認端口25 port = 25 #設置發件郵箱

sender = "yiwr@163.com"

#設置發件郵箱密碼 pwd = 'xxxx' #設置郵件接收人,發送給多人,隔開 receiver = '7894@qq.com'

#設置郵件抄送人,發送給多人,隔開 cc = '357@qq.com' ''' 不帶附件發送郵件 #設置html格式的郵件 #body = '

這是一個python測試郵件

test

' #msg = MIMEText(body, 'html') #設置正文為符合郵件格式的HTML內容 #發送普通格式郵件 msg = MIMEText('Python 普通格式,郵件發送測試...', 'plain', 'utf-8') ''' #附件方法實例 msg = MIMEMultipart() #設置頭信息 msg.add_header('subject', '測試郵件') #設置郵件標題 msg.add_header('from', sender) #設置發送人 msg.add_header('to', receiver) #設置接收人 msg.add_header('Cc',cc) # 抄送人 #設置正文內容 msg.attach(MIMEText('Python 郵件發送測試...', 'plain', 'utf-8')) #設置附件1,D://cs.txt 文件 att1 = MIMEText(open('D://cs.txt', 'rb').read(), 'base64', 'utf-8') att1.add_header('Content-Type', 'application/octet-stream') #這裡的filename可以任意寫,寫什麼名字,郵件中顯示附件的名字 att1.add_header('Content-Disposition', 'attachment', filename='cs.txt') msg.attach(att1) try: #注意!如果是使用SSL端口,這裡就要改為SMTP_SSL smtpObj = smtplib.SMTP(host, port) #登陸郵箱 smtpObj.login(sender, pwd) #發送郵件,注意第二個參數是發送人抄送人地址 smtpObj.sendmail(sender, receiver.split(',') + cc.split(','), msg.as_string()) print ("發送成功") except smtplib.SMTPException as e: print ("發送失敗") print(e) finally: smtpObj.quit()

發送之後結果截圖

三、問題

3.1 python通過qq郵箱,SMTP發送郵件失敗:

問題描述:使用qq賬戶及密碼SSL方式發送郵件,報錯:(535, b'Login Fail. Please enter your authorization code to login. More information in http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256')

解決方案:開啟POP3/SMTP服務,獲取授權碼,qq源碼的郵箱密碼改成授權碼即可

3.2 html附件變.bin文件後綴

問題描述:發送一個html格式的附件,收到郵件發送後綴變成.bin的文件

解決方案:把 att1["Content-Disposition"] = 'attachment; filename="' + "接口測試報告.html" 改為 att1.add_header('Content-Disposition', 'attachment', filename='接口測試報告.html')


[火星人 ] Python基於QQ郵箱實現SSL發送已經有436次圍觀

http://coctec.com/docs/python/shhow-post-231721.html