相信看到535報錯的同學代碼編寫方面都沒有問題,只是不明白為什么填寫了帳號密碼后還是報535錯誤,這里我以163郵箱為例,並使用Python講解怎么解決535問題
1. 先編寫一個最簡單的發郵件的python腳本
1 #coding: utf-8 2 import smtplib 3 from email.mime.text import MIMEText 4 from email.header import Header 5 6 sender = 'huochen1994@163.com' 7 receiver = 'huochen1994@126.com' 8 subject = 'python email test' 9 smtpserver = 'smtp.163.com' 10 username = 'huochen1994@126.com' 11 password = '*********' 12 13 msg = MIMEText( 'Hello Python', 'text', 'utf-8' ) 14 msg['Subject'] = Header( subject, 'utf-8' ) 15 16 smtp = smtplib.SMTP() 17 smtp.connect( smtpserver ) 18 smtp.login( username, password ) 19 smtp.sendmail( sender, receiver, msg.as_string() ) 20 smtp.quit()
2. 運行結果
1 Traceback (most recent call last): 2 File "mail.py", line 18, in <module> 3 smtp.login( username, password ) 4 File "/usr/lib64/python2.6/smtplib.py", line 589, in login 5 raise SMTPAuthenticationError(code, resp) 6 smtplib.SMTPAuthenticationError: (535, 'Error: authentication failed'
3. 解決方法
調用163郵箱服務器來發送郵件,我們需要開啟POP3/SMTP服務,這時163郵件會讓我們設置客戶端授權碼,這個授權碼替代上面代碼部分的passwd即可成功發送郵件
