Python基于QQ郵箱實(shí)現(xiàn)SSL發(fā)送
一、QQ郵箱SSL發(fā)送
獲取qq授權(quán)碼
ssl發(fā)送方式不是使用郵箱密碼,而是需要授權(quán)碼,具體步驟如下:
登錄發(fā)送人qq郵箱>>設(shè)置>>賬戶(hù)>>POP3/STMP服務(wù)開(kāi)啟>>生成授權(quán)碼
驗(yàn)證密保
復(fù)制16位授權(quán)碼
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#設(shè)置SMTP地址host = ’smtp.qq.com’#設(shè)置發(fā)件服務(wù)器端口號(hào),注意,這里有SSL和非SSL兩種形式,qq SSL端口為465,非SSL為端口默認(rèn)25port = '465'#設(shè)置發(fā)件郵箱sender = '357@qq.com'#設(shè)置發(fā)件郵箱的授權(quán)碼 ,qq郵箱ssl發(fā)送需要先開(kāi)啟stmp并獲取密碼 pwd = ’sqmqweertyuiioplk’ #16授權(quán)碼#設(shè)置郵件接收人,發(fā)送給多人,隔開(kāi) receiver = ’yiwr@163.com,7894@qq.com’ #設(shè)置郵件抄送人,發(fā)送給多人,隔開(kāi) cc = ’ywr198592@126.com’’’’ 不帶附件發(fā)送郵件#設(shè)置html格式的郵件#body = ’<h1>這是一個(gè)python測(cè)試郵件</h1><p>test</p>’ #msg = MIMEText(body, ’html’) # 設(shè)置正文為符合郵件格式的HTML內(nèi)容#發(fā)送普通格式郵件msg = MIMEText(’Python 普通格式,郵件發(fā)送測(cè)試...’, ’plain’, ’utf-8’)’’’#需要發(fā)送附件的方法實(shí)例msg = MIMEMultipart()#設(shè)置發(fā)送頭信息msg.add_header(’subject’, ’測(cè)試郵件’) #設(shè)置郵件標(biāo)題msg.add_header(’from’, sender) # 設(shè)置發(fā)送人msg.add_header(’to’, receiver) # 設(shè)置接收人msg.add_header(’Cc’,cc) # 抄送人#設(shè)置正文內(nèi)容msg.attach(MIMEText(’Python 郵件發(fā)送測(cè)試...’, ’plain’, ’utf-8’)) #設(shè)置附件1,D://cs.txt 文件att1 = MIMEText(open(’D://cs.txt’, ’rb’).read(), ’base64’, ’utf-8’)att1.add_header(’Content-Type’, ’application/octet-stream’)# 這里的filename可以任意寫(xiě),寫(xiě)什么名字,郵件中顯示附件的名字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ā)送郵件,注意第二個(gè)參數(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ā)送之后結(jié)果截圖
二、163郵箱非SSL發(fā)送
非ssl無(wú)需獲取授權(quán)碼,直接配置郵箱密碼即可
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 #設(shè)置SMTP地址host = ’smtp.163.com’#設(shè)置發(fā)件服務(wù)器端口號(hào)。注意,這里有SSL和非SSL兩種形式,非SSL默認(rèn)端口25port = 25#設(shè)置發(fā)件郵箱sender = 'yiwr@163.com'#設(shè)置發(fā)件郵箱密碼pwd = ’xxxx’ #設(shè)置郵件接收人,發(fā)送給多人,隔開(kāi) receiver = ’7894@qq.com’ #設(shè)置郵件抄送人,發(fā)送給多人,隔開(kāi) cc = ’357@qq.com’’’’ 不帶附件發(fā)送郵件#設(shè)置html格式的郵件#body = ’<h1>這是一個(gè)python測(cè)試郵件</h1><p>test</p>’ #msg = MIMEText(body, ’html’) #設(shè)置正文為符合郵件格式的HTML內(nèi)容#發(fā)送普通格式郵件msg = MIMEText(’Python 普通格式,郵件發(fā)送測(cè)試...’, ’plain’, ’utf-8’)’’’#附件方法實(shí)例msg = MIMEMultipart()#設(shè)置頭信息msg.add_header(’subject’, ’測(cè)試郵件’) #設(shè)置郵件標(biāo)題msg.add_header(’from’, sender) #設(shè)置發(fā)送人msg.add_header(’to’, receiver) #設(shè)置接收人msg.add_header(’Cc’,cc) # 抄送人#設(shè)置正文內(nèi)容msg.attach(MIMEText(’Python 郵件發(fā)送測(cè)試...’, ’plain’, ’utf-8’)) #設(shè)置附件1,D://cs.txt 文件att1 = MIMEText(open(’D://cs.txt’, ’rb’).read(), ’base64’, ’utf-8’)att1.add_header(’Content-Type’, ’application/octet-stream’)#這里的filename可以任意寫(xiě),寫(xiě)什么名字,郵件中顯示附件的名字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ā)送郵件,注意第二個(gè)參數(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ā)送之后結(jié)果截圖
三、問(wèn)題
3.1 python通過(guò)qq郵箱,SMTP發(fā)送郵件失敗:
問(wèn)題描述:使用qq賬戶(hù)及密碼SSL方式發(fā)送郵件,報(bào)錯(cuò):(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’)
解決方案:開(kāi)啟POP3/SMTP服務(wù),獲取授權(quán)碼,qq源碼的郵箱密碼改成授權(quán)碼即可
3.2 html附件變.bin文件后綴
問(wèn)題描述:發(fā)送一個(gè)html格式的附件,收到郵件發(fā)送后綴變成.bin的文件,如圖:
解決方案:把 att1['Content-Disposition'] = ’attachment; filename='’ + '接口測(cè)試報(bào)告.html' 改為 att1.add_header(’Content-Disposition’, ’attachment’, filename=’接口測(cè)試報(bào)告.html’)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)2. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法3. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法4. 得到XML文檔大小的方法5. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式6. 如何在jsp界面中插入圖片7. jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫(kù)的方法8. 利用CSS3新特性創(chuàng)建透明邊框三角9. XML入門(mén)的常見(jiàn)問(wèn)題(二)10. ASP常用日期格式化函數(shù) FormatDate()
