#coding: utf-8 import smtplibfrom email.mime.text import MIMETextfrom email.header import Headersender = ‘huochen1994@163.com‘receiver = ‘huochen1994@126.com‘subject = ‘python email test‘smtpserver = ‘smtp.163.com‘username = ‘huochen1994@126.com‘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()如果在上述代码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‘)调用163邮箱服务器来发送邮件,我们需要开启POP3/SMTP服务,这时163邮件会让我们设置客户端授权码,这个授权码替代上面代码部分的password即可成功发送邮件
原文:https://www.cnblogs.com/uvwill/p/10877181.html