DDR爱好者之家 Design By 杰米
smtplib模块负责发送邮件:是一个发送邮件的动作,连接邮箱服务器,登录邮箱,发送邮件(有发件人,收信人,邮件内容)。
email模块负责构造邮件:指的是邮箱页面显示的一些构造,如发件人,收件人,主题,正文,附件等。
email模块下有mime包,mime英文全称为“Multipurpose Internet Mail Extensions”,即多用途互联网邮件扩展,是目前互联网电子邮件普遍遵循的邮件技术规范。
该mime包下常用的有三个模块:text,image,multpart。
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header #邮件服务器信息 smtp_server = "smtp.qq.com" port = 465 # For starttls sender_email = "12345689@qq.com" password="" #get password from mailsetting #发送邮件信息,可以发送给多个收件人 receivers=["12345689@163.com","12345689@qq.com"] subject="This is import Python SMTP 邮件(文件传输) 多媒体测试" # message = MIMEText(text, "plain", "utf-8") #文本邮件 message = MIMEMultipart() message["Subject"] = Header(subject, "utf-8") message["from"] = sender_email message["to"] = ",".join(receivers) # 邮件正文内容 text=""" Dear Sir: how are you """ # message.attach(MIMEText('for detail information pls refer to attach1。\n The files you need are as followed. \n If you have any concern pls let me known. \n enjoy your weekend', 'plain', 'utf-8') message.attach(MIMEText(text,'plain','utf-8')) # 构造附件1 attach_file1='IMG1965.JPG' attach1 = MIMEText(open(attach_file1, 'rb').read(), 'base64', 'utf-8') attach1["Content-Type"] = 'application/octet-stream' attach1["Content-Disposition"] = 'attachment; filename={0}'.format(attach_file1) message.attach(attach1) # 构造附件2 attach_file2='YLJ.jpg' attach2 = MIMEText(open(attach_file2, 'rb').read(), 'base64', 'utf-8') attach2["Content-Type"] = 'application/octet-stream' attach2["Content-Disposition"] = 'attachment; filename={0}'.format(attach_file2) message.attach(attach2) # Try to log in to server and send email # server = smtplib.SMTP_SSL(smtp_server,port) server = smtplib.SMTP_SSL(smtp_server,port) try: server.login(sender_email, password) server.sendmail(sender_email,receivers,message.as_string()) print("邮件发送成功!!!") print("Mail with {0} & {1} has been send to {2} successfully.".format(attach_file1,attach_file2,receivers)) except Exception as e: # Print any error messages to stdout print("Error: 无法发送邮件") print(e) finally: server.quit()
结果
邮件发送成功!!!
Mail with IMG1965.JPG & IMG1963.jpg has been send to ['12345689@163.com', '12345689@qq.com'] successfully.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]