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, inserver.login('j_hao104@163.com', 'password') File "C:Python27libsmtplib.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, inserver.sendmail('j_hao104@163.com', ['946150454@qq.com'], msg.as_string()) File "C:Python27libsmtplib.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ā)件人不匹配。可以看出看出圖片中并沒有發(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' msg['Subject'] = Header(u'text', 'utf8').encode() msg['To'] = u'飛輪海 ' 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è)郵件", "Hello, It's test email.
")
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com