python 自动发送邮件
import smtplib from email.mime.text import MIMEText from email.header import Header #配置邮箱信息 mailHost="smtp.qq.com" mailUser="*******" mailPass="**********" #配置发送人和收件人信息 sender="********@qq.com" receivers=['********@qq.com'] #配置邮件内容 mailMessage= MIMEText("yetangjian前来报到","plain","utf-8") mailMessage['From']=Header("顽皮老板喝不醉",'utf-8') mailMessage['To']=Header("pangpang",'utf-8') mailMessage['subject']=Header("这是一封简单的自动发送邮件",'utf-8') #发送邮件 try: serve=smtplib.SMTP(mailHost)#调用服务器 serve.login(mailUser,mailPass)#登录你的服务账户 serve.sendmail(sender,receivers,mailMessage.as_string())#调用发件邮箱,收件邮箱,发送的内容 print("发送成功") except: print("发送失败") ———————————————————————————————————————————— import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import Header from email.mime.application import MIMEApplication import os #配置邮箱信息 mailHost="smtp.qq.com" mailUser="*******" mailPass="********" #配置发送人和收件人信息 sender="********@qq.com" receivers=['********@qq.com'] #配置邮件内容 def message_config(): content= """ <h2>全世界在你身后</h2> <a><img src="https://images0.cnblogs.com/blog/263119/201310/24152441-876bacfc45c1489d92854ddc3948b286.jpg"></a> """ content1= MIMEText(content,"html","utf-8") mailMessage=MIMEMultipart()#多个MIME对象 mailMessage.attach(content1)#添加内容 mailMessage['From']=Header("yetangjian",'utf-8')#发件人 mailMessage['To']=Header("pangpang",'utf-8')#收件人 mailMessage['subject']=Header("yetangjian",'utf-8')#主题 #添加附件 file_name='15To20work.pdf'#文件名 fail_path=os.path.join("C:/Users/Administrator/Desktop\python/"+file_name)#文件路径 pdf = MIMEApplication(open(fail_path,'rb').read())#打开pdf pdf["Content-Type"]="application/octet-stream"#设置内容类型 pdf.add_header('Content-Disposition','attachment',filename=file_name) mailMessage.attach(pdf) return mailMessage #发送邮件 def send_mail(mailMessage): try: mailMessage=message_config() serve=smtplib.SMTP(mailHost)#调用服务器 serve.login(mailUser,mailPass)#登录你的服务账户 serve.sendmail(sender,receivers,mailMessage.as_string())#调用发件邮箱,收件邮箱,发送的内容 print("发送成功") except: print("发送失败") if __name__ == "__main__": print("开始发送邮件") mailMessage=message_config()#调用配置 send_mail(mailMessage) print("发送完成")
作者: yetangjian
出处: https://www.cnblogs.com/yetangjian/p/15973474.html
关于作者: yetangjian
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可邮件(yetangjian@outlook.com)咨询.