python邮件发送---最终版本
具体看如下代码
1 #!/usr/bin/python 2 # -*- coding:utf-8 -*- 3 4 import smtplib #导入邮件发送协议SMTP包 5 from email.mime.text import MIMEText #发送文本邮件 6 from email.mime.application import MIMEApplication #发送带附件的邮件 7 from email.mime.multipart import MIMEMultipart #发送文本、图片、附件、html等多种正文内容的邮件 8 9 #邮件服务器基本信息 10 host = "smtp.qq.com" #邮件服务器地址 11 port = "465" #服务器端口号 12 user = "251906549@qq.com" #代理邮箱 13 password = "ycqnfseigpzwcbdh" #代理邮箱的授权码 14 15 #发邮件的基本信息 16 sender = user 17 receiver = ["251906549@qq.com","wangke5590@163.com"] 18 subject = "Python发送综合邮件测试!!!" 19 connect1 = "这是文本" 20 connect2 = """ 21 <p>这是html</p> 22 <a href='https://www.jd.com'>京东活动链接</a> 23 """ 24 connect3 = open(r"F:\workspace\baidu.html","rb").read() 25 connect4 = open(r"F:\workspace\cr7.jpg","rb").read() 26 connect5 = open(r"F:\workspace\Linux_learning.pdf","rb").read() 27 connect6 = open(r"F:\workspace\商报东路.m4a","rb").read() 28 29 #构造正文内容 30 msg1 = MIMEText(_text=connect1,_subtype="html",_charset="utf-8") 31 msg2 = MIMEText(_text=connect2,_subtype="html",_charset="utf-8") 32 msg3 = MIMEApplication(connect3) 33 msg3.add_header('content-disposition', 'attachment', filename='baidu.html') 34 msg4 = MIMEApplication(connect4) 35 msg4.add_header('content-disposition', 'attachment', filename='cr7.jpg') 36 msg5 = MIMEApplication(connect5) 37 msg5.add_header('content-disposition', 'attachment', filename='Linux_learning.pdf') 38 msg6 = MIMEApplication(connect6) 39 msg6.add_header('content-disposition', 'attachment', filename='商报东路.m4a') 40 41 #创建邮件发送对象,并给对象添加信息 42 message = MIMEMultipart() 43 message["From"] = sender 44 message["To"] = ";".join(receiver) 45 message["Subject"] = subject 46 for msg in [msg1,msg2,msg3,msg4,msg5,msg6]: #添加正文内容 47 message.attach(msg) 48 49 #发送邮件 50 con = smtplib.SMTP_SSL(host,port) 51 con.login(user,password) 52 con.sendmail(sender,receiver,message.as_string()) 53 con.quit()

浙公网安备 33010602011771号