Python 网易邮箱简单发送邮件

import smtplib   #导入PyEmail
from email.mime.text import MIMEText

msg_from="[email protected]"    #发件人邮箱
passwd="xyy0820"  #客户端授权密码,不是邮箱登陆密码
msg_to="[email protected]"  #接收人邮箱

subject="邮件主题"
content="邮件内容"
msg=MIMEText(content)
msg["Subject"]=subject
msg["From"]=msg_from
msg["To"]=msg_to

try:
    #s = smtplib.SMTP_SSL("smtp.163.com",465)
    s = smtplib.SMTP("smtp.163.com",25)
    s.login(msg_from, passwd)
    s.sendmail(msg_from, msg_to, msg.as_string())
    print ("发送成功")
except smtplib.SMTPException as e:
    print ("发送失败")
finally:
    s.quit()

代码如上,在运行过程中我们可能会遇到各种问题。

问题一、提示 import smtplib 报错  ,并且有Could not find a version that satisfies the requirement smtplib (from versions: )
No matching distribution found for smtplib
错误信息,是因为没有导入PyEamil第三方包。

问题二、提示ss.login(msg_from,passwd) 报错,并且有smtplib.SMTPAuthenticationError: (535, b'Error: authentication failed' 错误信息,是因为passwd是客户端授权密码,不是邮箱登陆密码。

问题三、邮件的成功的发送需要开启邮件设置

其他链接帮助:https://www.cnblogs.com/secondtonone1/p/8213749.html   网易邮箱发送邮件升级代码

                        https://blog.csdn.net/dearmorning/article/details/81069075   网易邮箱发送邮件问题帮助

猜你喜欢

转载自blog.csdn.net/tjfsuxyy/article/details/87857452