發郵件的python腳本
阿新 • • 發佈:2019-05-16
客戶端 divider 發郵件 usr 讓我 郵箱服務器 conn -s tex 和
1. 編寫一個最簡單的發郵件的python腳本
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = ‘[email protected]‘
receiver = ‘[email protected]‘
subject = ‘python email test‘
smtpserver = ‘smtp.163.com‘
username = ‘[email protected]‘
password = ‘*********‘
msg = MIMEText( ‘Hello Python‘, ‘text‘, ‘utf-8‘ )
msg[‘Subject‘] = Header( subject, ‘utf-8‘ )
smtp = smtplib.SMTP()
smtp.connect( smtpserver )
smtp.login( username, password )
smtp.sendmail( sender, receiver, msg.as_string() )
smtp.quit()
2. 運行結果
如果在上述代碼username
password
,變量中填寫郵箱的帳號密碼那麽會看到以下報錯:
Traceback (most recent call last):
File "mail.py", line 18, in <module>
smtp.login( username, password )
File "/usr/lib64/python2.6/smtplib.py", line 589, in login
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, ‘Error: authentication failed‘)
3. 解決方法
調用163郵箱服務器來發送郵件,我們需要開啟POP3/SMTP服務,這時163郵件會讓我們設置客戶端授權碼,這個授權碼替代上面代碼部分的password
即可成功發送郵件
發郵件的python腳本