搜索
您的当前位置:首页python发送邮件测试报告

python发送邮件测试报告

来源:乌哈旅游

发邮件需要用到python两个模块,smtplib和email,这俩模块是python自带的,只需import即可使用。smtplib模块主要负责发送邮件,email模块主要负责构造邮件。其中MIMEText()定义邮件正文,Header()定义邮件标题。MIMEMulipart模块构造带附件

发送HTML格式的邮件:

send_email_html.py

[python]   
 

发送带附件的邮件

send_email_file.py

[python]   
 
  1. import smtplib  
  2. from email.mime.text import MIMEText            #MIMRText()定义邮件正文  
  3. from email.mime.multipart import MIMEMultipart  #MIMEMulipart模块构造带附件  
  4.   
  5. #发送邮件的服务器  
  6. smtpserver = 'smtp.sina.com'  
  7.   
  8. #发送邮件用户和密码  
  9. user ="xxx@sina.com"  
  10. password = "xxx"  
  11.   
  12. #发送者  
  13. sender = "xxx@sina.com"  
  14.   
  15. #接收者  
  16. receiver = "1xxx@qq.com"  
  17.   
  18. #邮件主题  
  19. subject = "附件的邮件"  
  20.   
  21. #发送附件  
  22. sendfile = open("C:\\Users\\Administrator\\Desktop\\html5.txt","r").read()  
  23.   
  24. att = MIMEText(sendfile,"base64","utf-8")  
  25. att["Content-Type"] = "application/octet-stream"  
  26. att["Content-Disposition"] = "attachment;filename = 'html5.txt'"  
  27.   
  28. msgRoot = MIMEMultipart('related')  
  29. msgRoot['Subject'] = subject  
  30. msgRoot.attach(att)  
  31.   
  32. smtp = smtplib.SMTP()  
  33. smtp.connect(smtpserver)  
  34. smtp.login(user,password)  
  35. smtp.sendmail(sender,receiver,msgRoot.as_string())  
  36. smtp.quit()  

因篇幅问题不能全部显示,请点此查看更多更全内容

Top