Python基于QQ郵箱實現(xiàn)SSL發(fā)送
一、QQ郵箱SSL發(fā)送
獲取qq授權碼
ssl發(fā)送方式不是使用郵箱密碼,而是需要授權碼,具體步驟如下:
登錄發(fā)送人qq郵箱>>設置>>賬戶>>POP3/STMP服務開啟>>生成授權碼
驗證密保
復制16位授權碼
qq郵箱發(fā)送源碼
#!/usr/bin/python3# encoding:utf-8’’’Created on 2020-04-24 12:15@author: Administrator’’’#coding:utf-8import smtplibfrom email.mime.text import MIMEText # 引入smtplib和MIMETextfrom email.mime.multipart import MIMEMultipart#設置SMTP地址host = ’smtp.qq.com’#設置發(fā)件服務器端口號,注意,這里有SSL和非SSL兩種形式,qq SSL端口為465,非SSL為端口默認25port = '465'#設置發(fā)件郵箱sender = '[email protected]'#設置發(fā)件郵箱的授權碼 ,qq郵箱ssl發(fā)送需要先開啟stmp并獲取密碼 pwd = ’sqmqweertyuiioplk’ #16授權碼#設置郵件接收人,發(fā)送給多人,隔開 receiver = ’[email protected],[email protected]’ #設置郵件抄送人,發(fā)送給多人,隔開 cc = ’[email protected]’’’’ 不帶附件發(fā)送郵件#設置html格式的郵件#body = ’<h1>這是一個python測試郵件</h1><p>test</p>’ #msg = MIMEText(body, ’html’) # 設置正文為符合郵件格式的HTML內容#發(fā)送普通格式郵件msg = MIMEText(’Python 普通格式,郵件發(fā)送測試...’, ’plain’, ’utf-8’)’’’#需要發(fā)送附件的方法實例msg = MIMEMultipart()#設置發(fā)送頭信息msg.add_header(’subject’, ’測試郵件’) #設置郵件標題msg.add_header(’from’, sender) # 設置發(fā)送人msg.add_header(’to’, receiver) # 設置接收人msg.add_header(’Cc’,cc) # 抄送人#設置正文內容msg.attach(MIMEText(’Python 郵件發(fā)送測試...’, ’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) #發(fā)送郵件,注意第二個參數(shù)是發(fā)送人抄送人地址 smtpObj.sendmail(sender, receiver.split(’,’) + cc.split(’,’), msg.as_string()) print ('發(fā)送成功')except smtplib.SMTPException as e: print ('發(fā)送失敗') print(e)finally: smtpObj.quit()
發(fā)送之后結果截圖
二、163郵箱非SSL發(fā)送
非ssl無需獲取授權碼,直接配置郵箱密碼即可
163郵箱發(fā)送源碼
#!/usr/bin/python3#encoding:utf-8’’’Created on 2020-04-24 12:15@author: Administrator’’’#coding:utf-8import smtplibfrom email.mime.text import MIMEText #引入smtplib和MIMETextfrom email.mime.multipart import MIMEMultipart #設置SMTP地址host = ’smtp.163.com’#設置發(fā)件服務器端口號。注意,這里有SSL和非SSL兩種形式,非SSL默認端口25port = 25#設置發(fā)件郵箱sender = '[email protected]'#設置發(fā)件郵箱密碼pwd = ’xxxx’ #設置郵件接收人,發(fā)送給多人,隔開 receiver = ’[email protected]’ #設置郵件抄送人,發(fā)送給多人,隔開 cc = ’[email protected]’’’’ 不帶附件發(fā)送郵件#設置html格式的郵件#body = ’<h1>這是一個python測試郵件</h1><p>test</p>’ #msg = MIMEText(body, ’html’) #設置正文為符合郵件格式的HTML內容#發(fā)送普通格式郵件msg = MIMEText(’Python 普通格式,郵件發(fā)送測試...’, ’plain’, ’utf-8’)’’’#附件方法實例msg = MIMEMultipart()#設置頭信息msg.add_header(’subject’, ’測試郵件’) #設置郵件標題msg.add_header(’from’, sender) #設置發(fā)送人msg.add_header(’to’, receiver) #設置接收人msg.add_header(’Cc’,cc) # 抄送人#設置正文內容msg.attach(MIMEText(’Python 郵件發(fā)送測試...’, ’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) #發(fā)送郵件,注意第二個參數(shù)是發(fā)送人抄送人地址 smtpObj.sendmail(sender, receiver.split(’,’) + cc.split(’,’), msg.as_string()) print ('發(fā)送成功')except smtplib.SMTPException as e: print ('發(fā)送失敗') print(e)finally: smtpObj.quit()
發(fā)送之后結果截圖
三、問題
3.1 python通過qq郵箱,SMTP發(fā)送郵件失敗:
問題描述:使用qq賬戶及密碼SSL方式發(fā)送郵件,報錯:(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文件后綴
問題描述:發(fā)送一個html格式的附件,收到郵件發(fā)送后綴變成.bin的文件,如圖:
解決方案:把 att1['Content-Disposition'] = ’attachment; filename='’ + '接口測試報告.html' 改為 att1.add_header(’Content-Disposition’, ’attachment’, filename=’接口測試報告.html’)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. 基于 Python 實踐感知器分類算法2. Python如何批量生成和調用變量3. ASP.Net Core對USB攝像頭進行截圖4. ajax動態(tài)加載json數(shù)據(jù)并詳細解析5. Python 中如何使用 virtualenv 管理虛擬環(huán)境6. python利用opencv實現(xiàn)顏色檢測7. 通過CSS數(shù)學函數(shù)實現(xiàn)動畫特效8. ASP.Net Core(C#)創(chuàng)建Web站點的實現(xiàn)9. ASP.NET MVC實現(xiàn)橫向展示購物車10. windows服務器使用IIS時thinkphp搜索中文無效問題
