杭州专业设计网站,网站升级建设,合肥网络推广服务公司,网站开发项目计划书模板最近用到Python自动发送邮件#xff0c;主要就是三步#xff0c;登录邮件、写邮件内容、发送#xff0c;用到的库是 smtplib 和 email#xff0c;直接使用pip安装即可我使用的是QQ邮箱#xff0c;首先需要设置QQ邮箱POP3/SMTP服务记住这个授权码#xff0c;这个授权码就是…最近用到Python自动发送邮件主要就是三步登录邮件、写邮件内容、发送用到的库是 smtplib 和 email直接使用pip安装即可我使用的是QQ邮箱首先需要设置QQ邮箱POP3/SMTP服务记住这个授权码这个授权码就是Python脚本中登录邮箱时的密码而不是你平时登录邮箱时的那个密码一.发送普通文本邮件#发送多种类型的邮件from email.mime.multipart import MIMEMultipartmsg_from 1508691067qq.com # 发送方邮箱passwd xxx #就是上面的授权码to [1508691067qq.com] #接受方邮箱#设置邮件内容#MIMEMultipart类可以放任何内容msg MIMEMultipart()conntent这个是字符串#把内容加进去msg.attach(MIMEText(conntent,plain,utf-8))#设置邮件主题msg[Subject]这个是邮件主题#发送方信息msg[From]msg_from#开始发送#通过SSL方式发送服务器地址和端口s smtplib.SMTP_SSL(smtp.qq.com, 465)# 登录邮箱s.login(msg_from, passwd)#开始发送s.sendmail(msg_from,to,msg.as_string())print(邮件发送成功)二.发送携带附件的邮件import smtplibfrom email.mime.text import MIMEText#发送多种类型的邮件from email.mime.multipart import MIMEMultipartmsg_from 1508691067qq.com # 发送方邮箱passwd xxxxxto [1508691067qq.com] #接受方邮箱#设置邮件内容#MIMEMultipart类可以放任何内容msg MIMEMultipart()conntent这个是字符串#把内容加进去msg.attach(MIMEText(conntent,plain,utf-8))#添加附件att1MIMEText(open(result.xlsx,rb).read(),base64,utf-8) #打开附件att1[Content-Type]application/octet-stream #设置类型是流媒体格式att1[Content-Disposition]attachment;filenameresult.xlsx #设置描述信息msg.attach(att1) #加入到邮件中#设置邮件主题msg[Subject]这个是邮件主题#发送方信息msg[From]msg_from#开始发送#通过SSL方式发送服务器地址和端口s smtplib.SMTP_SSL(smtp.qq.com, 465)# 登录邮箱s.login(msg_from, passwd)#开始发送s.sendmail(msg_from,to,msg.as_string())print(邮件发送成功)三.发送携带图片的附件同理可以使用上面的方法也可以发送图片附件import smtplibfrom email.mime.text import MIMEText#发送多种类型的邮件from email.mime.multipart import MIMEMultipartmsg_from 1508691067qq.com # 发送方邮箱passwd xxxxxto [1508691067qq.com] #接受方邮箱#设置邮件内容#MIMEMultipart类可以放任何内容msg MIMEMultipart()conntent这个是字符串#把内容加进去msg.attach(MIMEText(conntent,plain,utf-8))#添加附件att1MIMEText(open(result.xlsx,rb).read(),base64,utf-8) #打开附件att1[Content-Type]application/octet-stream #设置类型是流媒体格式att1[Content-Disposition]attachment;filenameresult.xlsx #设置描述信息att2MIMEText(open(1.jpg,rb).read(),base64,utf-8)att2[Content-Type]application/octet-stream #设置类型是流媒体格式att2[Content-Disposition]attachment;filename1.jpg #设置描述信息msg.attach(att1) #加入到邮件中msg.attach(att2)#设置邮件主题msg[Subject]这个是邮件主题#发送方信息msg[From]msg_from#开始发送#通过SSL方式发送服务器地址和端口s smtplib.SMTP_SSL(smtp.qq.com, 465)# 登录邮箱s.login(msg_from, passwd)#开始发送s.sendmail(msg_from,to,msg.as_string())print(邮件发送成功)四.发送 html 格式的邮件import smtplibfrom email.mime.text import MIMEText#发送多种类型的邮件from email.mime.multipart import MIMEMultipartimport datetimemsg_from 1508691067qq.com # 发送方邮箱passwd xxxxxxto [1508691067qq.com] #接受方邮箱#设置邮件内容#MIMEMultipart类可以放任何内容msg MIMEMultipart()# conntent这个是字符串# #把内容加进去# msg.attach(MIMEText(conntent,plain,utf-8))#添加附件att1MIMEText(open(result.xlsx,rb).read(),base64,utf-8) #打开附件att1[Content-Type]application/octet-stream #设置类型是流媒体格式att1[Content-Disposition]attachment;filenameresult.xlsx #设置描述信息att2MIMEText(open(1.jpg,rb).read(),base64,utf-8)att2[Content-Type]application/octet-stream #设置类型是流媒体格式att2[Content-Disposition]attachment;filename1.jpg #设置描述信息msg.attach(att1) #加入到邮件中msg.attach(att2)now_time datetime.datetime.now()year now_time.yearmonth now_time.monthday now_time.daymytime str(year) 年 str(month) 月 str(day) 日 fayanren爱因斯坦zhuchiren牛顿#构造HTMLcontent 这个是标题xxxx通知您好以下内容是本次会议的纪要,请查收发言人{fayanren}主持人{zhuchiren}{mytime}.format(fayanrenfayanren, zhuchirenzhuchiren, mytimemytime)msg.attach(MIMEText(content,html,utf-8))#设置邮件主题msg[Subject]这个是邮件主题#发送方信息msg[From]msg_from#开始发送#通过SSL方式发送服务器地址和端口s smtplib.SMTP_SSL(smtp.qq.com, 465)# 登录邮箱s.login(msg_from, passwd)#开始发送s.sendmail(msg_from,to,msg.as_string())print(邮件发送成功)以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持。标签QQ,qq,python,发送,MIMEText,msg,邮箱,邮件来源 https://blog.51cto.com/14825302/2544811