標籤: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郵箱)