【selenium+Python unittest】之發送郵箱時報錯:smtplib.SMTPDataError、smtplib.SMTPAuthenticationError(例:126郵箱)

來源:互聯網
上載者:User

標籤:login   pass   post   log   http   tps   str   nec   one   

原代碼如下:

import smtplibfrom email.mime.text import MIMETextfrom email.header import Header#要發送的伺服器smtpserver = ‘smtp.126.com‘#要發送的信箱使用者名/密碼user = ‘[email protected]‘password = ‘XXX‘#發送的郵箱sender = ‘[email protected]‘#接收的郵箱receiver = ‘[email protected]‘#發送郵箱主題subject = ‘test_mail‘#編寫HTML類型的郵件內文msg = MIMEText(‘<html><h1>大佬好!</h1></html>‘,‘html‘,‘utf-8‘)msg[‘Subject‘] = Header(subject,‘utf-8‘)#串連發送郵件smtp = smtplib.SMTP()smtp.connect(smtpserver)smtp.login(user,password)smtp.sendmail(sender,receiver,msg.as_string())smtp.quit()

 

一、現象:

發送郵件時,運行時報錯smtplib.SMTPDataError,如:

 

二、解決辦法

①經網上查詢得知:因為126郵箱中沒有開啟【授權碼】,如所示應該開啟:

 ②但是再次運行代碼還是報錯:smtplib.SMTPAuthenticationError,如,提示登陸失敗:

原因是:代碼中的密碼應該改為授權密碼即可。

③繼續運行後,但是代碼還是報錯:smtplib.SMTPDataError:(554, b‘DT:SPM 126 smtp4

 

報錯原因是沒有加上下面的代碼:

#報錯原因是因為“寄件者和收件者參數沒有進行定義msg[‘from‘] = ‘[email protected]‘msg[‘to‘] = ‘[email protected]‘

 

加上之後,終於解決發送郵件失敗的問題了。

 

完整代碼如下:(因保密自行替換)

 

import smtplibfrom email.mime.text import MIMETextfrom email.header import Header#要發送的伺服器smtpserver = ‘smtp.126.com‘#要發送的信箱使用者名/密碼user = ‘[email protected]‘password = ‘XXX‘#發送的郵箱sender = ‘[email protected]‘#接收的郵箱receiver = ‘[email protected]‘#發送郵箱主題subject = ‘test_mail‘#編寫HTML類型的郵件內文msg = MIMEText(‘<html><h1>大佬好!</h1></html>‘,‘html‘,‘utf-8‘)msg[‘Subject‘] = Header(subject,‘utf-8‘)msg[‘from‘] = ‘[email protected]‘msg[‘to‘] = ‘[email protected]‘#串連發送郵件smtp = smtplib.SMTP()smtp.connect(smtpserver)smtp.login(user,password)smtp.sendmail(sender,receiver,msg.as_string())smtp.quit()

 

【selenium+Python unittest】之發送郵箱時報錯:smtplib.SMTPDataError、smtplib.SMTPAuthenticationError(例:126郵箱)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.