这里是用smtplib实现发送qq邮件,首先需要获取qq邮箱的授权码,这里就不再赘述,大家可百度哈
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
#==============定义发送邮件 ===============
class SendMail():
def sendmail(self,filename):
#发送邮箱服务器
smtpserver='smtp.qq.com'
#发送邮箱用户/密码
user='发送人邮箱'
password='发送人授权码'
#发送邮箱
sender='发送人邮箱'
#接收邮箱
receiver='收件人邮箱'
#发送邮件主题
subject='python email'
#发送的附件
sendfile=open(filename,'rb').read()
att=MIMEText(sendfile,'base64','utf-8')
att["Content-Type"]='application/octet-stram'
att["content-Disposition"]='attachment;filename="result.html"'
msgRoot=MIMEMultipart('related')
msgRoot['Subject']=subject
msgRoot.attach(att)
#连接发送邮件
smtp=smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user,password)
smtp.sendmail(sender,receiver,msgRoot.as_string())
smtp.quit()
print('邮件发送成功!')
然后在主类中调用该方法:
可参考我写的 HtmlTextRunnerNew 的博客,里面有调用该方法的代码
最后,附上发送邮件成功图片