Python使用smtplib模塊發(fā)送電子郵件的流程詳解
1、登錄SMTP服務(wù)器
首先使用網(wǎng)上的方法(這里使用163郵箱,smtp.163.com是smtp服務(wù)器地址,25為端口號(hào)):
import smtplib
server = smtplib.SMTP('smtp.163.com', 25)
server.login('j_hao104@163.com', 'password')
Traceback (most recent call last):
File "C:/python/t.py", line 192, in <module>
server.login('j_hao104@163.com', 'password')
File "C:\Python27\lib\smtplib.py", line 622, in login
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, 'Error: authentication failed')
發(fā)現(xiàn)返回:
smtplib.SMTPAuthenticationError: (535, 'Error: authentication failed')
,提示驗(yàn)證失敗。
有說python不支持SMTP服務(wù),或是服務(wù)沒開啟之類的。但是我想起上次我用foxmail登錄我的163郵箱的時(shí)候,郵箱密碼都輸對(duì)了還是提示我密碼錯(cuò)誤,最后的解決辦法是:像QQ和163郵箱現(xiàn)在都有個(gè)客戶端密碼,用第三方登錄時(shí)需用客戶端密碼登錄才行,python也是如此,因此去設(shè)置好客戶端密碼,再用客戶端密碼登錄。

import smtplib
server = smtplib.SMTP('smtp.163.com', 25)
server.login('j_hao104@163.com', 'clientPassword')
此時(shí)便返回登錄成功提示:
(235, 'Authentication successful')
2、發(fā)送郵件
首先使用網(wǎng)上給出的代碼:
import smtplib
from email.mime.text import MIMEText
server = smtplib.SMTP('smtp.163.com', 25)
server.login('j_hao104@163.com', 'clientPassword')
msg = MIMEText('hello, send by Python...', 'plain', 'utf-8')
server.sendmail('j_hao104@163.com', ['946150454@qq.com'], msg.as_string())
構(gòu)造MIMEText對(duì)象時(shí),第一個(gè)參數(shù)是郵件正文,第二個(gè)參數(shù)是MIME的subtype,最后個(gè)是編碼方式。
sendmail是發(fā)郵件方法,第一個(gè)參數(shù)是發(fā)件郵箱,第二個(gè)參數(shù)是收件人郵箱,是一個(gè)列表,代表可以同時(shí)發(fā)給多個(gè)人,as_string是把MIMEText對(duì)象變成str。
但是執(zhí)行結(jié)果并不能得到網(wǎng)上說的結(jié)果:

而是:
Traceback (most recent call last):
File "C:/python/t.py", line 195, in <module>
server.sendmail('j_hao104@163.com', ['946150454@qq.com'], msg.as_string())
File "C:\Python27\lib\smtplib.py", line 746, in sendmail
raise SMTPDataError(code, resp)
smtplib.SMTPDataError: (554, 'DT:SPM 163 smtp11,D8CowEDpDkE427JW_wQIAA--.4996S2 1454562105,please see http://mail.163.com/help/help_spam_16.htm?ip=171.221.144.51&hostid=smtp11&time=1454562105')
網(wǎng)上一查才知道:smtplib.SMTPDataError: (554, 'DT:SPM 163 smtp11……的錯(cuò)誤是因?yàn)樾欧獍l(fā)件人和信頭發(fā)件人不匹配??梢钥闯隹闯鰣D片中并沒有發(fā)件人和主題,所以需要對(duì)代碼做如下修改:
import smtplib
from email.header import Header
from email.mime.text import MIMEText
server = smtplib.SMTP('smtp.163.com', 25)
server.login('j_hao104@163.com', 'clientPassword')
msg = MIMEText('hello, send by Python...', 'plain', 'utf-8')
msg['From'] = 'j_hao104@163.com <j_hao104@163.com>'
msg['Subject'] = Header(u'text', 'utf8').encode()
msg['To'] = u'飛輪海 <jinghao5849312@qq.com>'
server.sendmail('j_hao104@163.com', ['946150454@qq.com'], msg.as_string())
這樣就能成功發(fā)出郵件啦
msg里的具體信息可以用一般發(fā)郵件方式發(fā)封郵件測(cè)試下

3、參考示例
import smtplib
from email.mime.text import MIMEText
to_list = ['123@123.com', '456@456.com']
server_host = 'smtp.163.com'
username = '你的郵箱賬號(hào)'
password = '你的郵箱密碼'
def send(to_list, sub, content):
'''
:param to_list: 收件人郵箱
:param sub: 郵件標(biāo)題
:param content: 內(nèi)容
'''
me = "manager" + "<" + username + ">"
# _subtype 可以設(shè)為html,默認(rèn)是plain
msg = MIMEText(content, _subtype='html')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ';'.join(to_list)
try:
server = smtplib.SMTP()
server.connect(server_host)
server.login(username, password)
server.sendmail(me, to_list, msg.as_string())
server.close()
except Exception as e:
print str(e)
if __name__ == '__main__':
send(to_list, "這個(gè)是一個(gè)郵件", "<h1>Hello, It's test email.</h1>")
相關(guān)文章
基于Python實(shí)現(xiàn)帕累托圖的示例詳解
帕累托圖是一種特殊的直方圖, 在項(xiàng)目管理知識(shí)體系中屬于質(zhì)量管理的工具。本文為大家整理了Python實(shí)現(xiàn)帕累托圖的方法,需要的可以參考一下2023-03-03
python實(shí)現(xiàn)字符串加密成純數(shù)字
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)字符串加密成純數(shù)字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
python數(shù)據(jù)抓取分析的示例代碼(python + mongodb)
本篇文章主要介紹了python數(shù)據(jù)抓取分析的示例代碼(python + mongodb),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-12-12
Python smtplib實(shí)現(xiàn)發(fā)送郵件功能
這篇文章主要為大家詳細(xì)介紹了Python smtplib實(shí)現(xiàn)發(fā)送郵件功能,包含文本、附件、圖片等,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
python opencv 二值化 計(jì)算白色像素點(diǎn)的實(shí)例
今天小編就為大家分享一篇python opencv 二值化 計(jì)算白色像素點(diǎn)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-07-07
Python 根據(jù)日志級(jí)別打印不同顏色的日志的方法示例
這篇文章主要介紹了Python 根據(jù)日志級(jí)別打印不同顏色的日志的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
python基礎(chǔ)之Numpy庫(kù)中array用法總結(jié)
NumPy(Numerical Python的縮寫)是一個(gè)開源的Python科學(xué)計(jì)算庫(kù),使用NumPy就可以很自然地使用數(shù)組和矩陣,這篇文章主要給大家介紹了關(guān)于python基礎(chǔ)之Numpy庫(kù)中array用法的相關(guān)資料,需要的朋友可以參考下2021-08-08
使用Python的OpenCV模塊識(shí)別滑動(dòng)驗(yàn)證碼的缺口(推薦)
這篇文章主要介紹了使用Python的OpenCV模塊識(shí)別滑動(dòng)驗(yàn)證碼的缺口,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05
Python符號(hào)計(jì)算之實(shí)現(xiàn)函數(shù)極限的方法
這篇文章主要介紹了Python符號(hào)計(jì)算之實(shí)現(xiàn)函數(shù)極限的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

