Guest User

Untitled

a guest
Dec 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3.  
  4. import smtplib
  5. from email.mime.text import MIMEText
  6. from email.mime.multipart import MIMEMultipart
  7. from email.header import Header
  8.  
  9. sender = 'from@runoob.com'
  10. receivers = ['429240967@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
  11.  
  12. #创建一个带附件的实例
  13. message = MIMEMultipart()
  14. message['From'] = Header("菜鸟教程", 'utf-8')
  15. message['To'] = Header("测试", 'utf-8')
  16. subject = 'Python SMTP 邮件测试'
  17. message['Subject'] = Header(subject, 'utf-8')
  18.  
  19. #邮件正文内容
  20. message.attach(MIMEText('这是菜鸟教程Python 邮件发送测试……', 'plain', 'utf-8'))
  21.  
  22. # 构造附件1,传送当前目录下的 test.txt 文件
  23. att1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8')
  24. att1["Content-Type"] = 'application/octet-stream'
  25. # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
  26. att1["Content-Disposition"] = 'attachment; filename="test.txt"'
  27. message.attach(att1)
  28.  
  29. # 构造附件2,传送当前目录下的 runoob.txt 文件
  30. att2 = MIMEText(open('runoob.txt', 'rb').read(), 'base64', 'utf-8')
  31. att2["Content-Type"] = 'application/octet-stream'
  32. att2["Content-Disposition"] = 'attachment; filename="runoob.txt"'
  33. message.attach(att2)
  34.  
  35. try:
  36. smtpObj = smtplib.SMTP('localhost')
  37. smtpObj.sendmail(sender, receivers, message.as_string())
  38. print "邮件发送成功"
  39. except smtplib.SMTPException:
  40. print "Error: 无法发送邮件"
Add Comment
Please, Sign In to add comment