Python自动发邮件(QQ为例)

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart

mail_user="xxxxxxxx@qq.com"
mail_pass="lxtauunzcvbbfb"#授权码,邮箱->设置->账号->smt,ssl下面开启发短信获取

sender="xxxxxxxx@qq.com" receivers="bbbbbbbb@qq.com" message=MIMEMultipart()#传附件才多这一步,指mime多部份,不用MIMEmultipart就只能传一个内容 message['From']=Header("xxxxxxxx@qq.com") #寄件者名字:xxxxxxxx@qq.com message['To']=Header("收到",'utf-8') #寄件者名字:收到 subject='第一份自动发的邮件' message['Subject']=Header(subject,'utf-8') #邮件主旨 #下面是插入附件: message.attach(MIMEText('来,给你一个链接:http://www.baidu.com','plain','utf-8'))#传内容,下面是传附件 att1 = MIMEText(open('C:/Users/Curry/Pictures/xxx.png', 'rb').read(), 'base64', 'utf-8') att1["Content-Type"] = 'application/octet-stream'#定义附件类型 # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 att1["Content-Disposition"] = 'attachment; filename=" .png"' message.attach(att1) try: ss=smtplib.SMTP_SSL("smtp.qq.com",465)#qq邮箱的主机和端口 ss.login(mail_user,mail_pass)#登录寄件者的账号密码(smtp授权码) ss.sendmail(sender,[receivers],message.as_string())#寄件者,收件者,可以是多个所以是list,前面所有的message print("成功") ss.quit() except smtplib.SMTPException: print("发生失败")

 

posted @ 2020-08-08 23:35  Curry!  阅读(158)  评论(0编辑  收藏  举报