import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
mail_user="[email protected]"
mail_pass="lxtauunzcvbbfb"#授权码,邮箱->设置->账号->smt,ssl下面开启发短信获取
sender="[email protected]"
receivers="[email protected]"
message=MIMEMultipart()#传附件才多这一步,指mime多部份,不用MIMEmultipart就只能传一个内容
message['From']=Header("[email protected]") #寄件者名字:[email protected]
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("发生失败")