美文网首页
python 邮件自动发送采坑记录

python 邮件自动发送采坑记录

作者: SugeonYen | 来源:发表于2019-07-02 11:05 被阅读0次

import smtplib

from email.mime.textimport MIMEText

from email.headerimport Header

# 第三方 SMTP 服务

mail_host= "smtp.qq.com"  # 设置服务器

mail_user= "1xxxx@qq.com"  # 用户名

mail_pass= "xxxxxx"  # 口令

sender= '1xxxx@qq.com'

receivers= ['1xxxxxx@wo.cn']# 接收邮件,可设置为你的QQ邮箱或者其他邮箱

message= MIMEText('Python 邮件发送测试...','plain','utf-8')

message['From']= Header("菜鸟教程",'utf-8')

message['To']= Header("测试",'utf-8')

subject= 'Python SMTP 邮件测试'

message['Subject']= Header(subject,'utf-8')

#smtpObj= smtplib.SMTP()

#smtpObj.connect(email_host,25)

#抛出异常:smtplib.SMTPServerDisconnected: Connection unexpectedly closed

#QQ邮箱是支持安全邮件的,需要通过SSL发送的邮件,如下:

smtpObj= smtplib.SMTP_SSL(mail_host)

smtpObj.connect(mail_host,465)# 465 为 SMTP 端口号

smtpObj.login(mail_user, mail_pass)

smtpObj.sendmail(sender, receivers, message.as_string())

print("邮件发送成功")

1.python 3.7 修改了 ssl.py 导致了 smtplib.SMTP_SSL 的问题,如果按照原来的 报错:server_hostname cannot be an empty string or start with a leading dot

解决办法:smtplib.SMTP(mail_host).connect(mail_host,465) 

                    原写法:smtplib.SMTP().connect(mail_host,465) 

2.支持安全邮件的,需要通过SSL发送的邮件。 抛出异常:smtplib.SMTPServerDisconnected: Connection unexpectedly closed

解决办法:smtplib.SMTP_SSL(mail_host).connect(mail_host,465)

                原写法:smtplib.SMTP(mail_host).connect(mail_host,465)

3.password必须是授权码(授权码的问题自行百度相应邮箱)

相关文章

网友评论

      本文标题:python 邮件自动发送采坑记录

      本文链接:https://www.haomeiwen.com/subject/pfgndqtx.html