python发邮件实例

引用:http://blog.csdn.net/wyuan8913/article/details/6917873

环境:windows ,python3.2.2

文件形式的邮件

[python] view plaincopy

  1. #!/usr/bin/env python3
  2. #coding: utf-8
  3. import smtplib 
  4. from email.mime.text import MIMEText 
  5. from email.header import Header 
  6. sender = '***'
  7. receiver = '***'
  8. subject = 'python email test'
  9. smtpserver = 'smtp.163.com'
  10. username = '***'
  11. password = '***'
  12. msg = MIMEText('你好','plain','utf-8')#中文需参数‘utf-8’,单字节字符不需要
  13. msg['Subject'] = Header(subject, 'utf-8') 
  14. smtp = smtplib.SMTP() 
  15. smtp.connect('smtp.163.com') 
  16. smtp.login(username, password) 
  17. smtp.sendmail(sender, receiver, msg.as_string()) 
  18. smtp.quit() 

HTML形式的邮件

[python] view plaincopy

  1. #!/usr/bin/env python3
  2. #coding: utf-8
  3. import smtplib 
  4. from email.mime.text import MIMEText 
  5. sender = '***'
  6. receiver = '***'
  7. subject = 'python email test'
  8. smtpserver = 'smtp.163.com'
  9. username = '***'
  10. password = '***'
  11. msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8') 
  12. msg['Subject'] = subject 
  13. smtp = smtplib.SMTP() 
  14. smtp.connect('smtp.163.com') 
  15. smtp.login(username, password) 
  16. smtp.sendmail(sender, receiver, msg.as_string()) 
  17. smtp.quit() 

带图片的HTML邮件

[python] view plaincopy

  1. #!/usr/bin/env python3
  2. #coding: utf-8
  3. import smtplib 
  4. from email.mime.multipart import MIMEMultipart 
  5. from email.mime.text import MIMEText 
  6. from email.mime.image import MIMEImage 
  7. sender = '***'
  8. receiver = '***'
  9. subject = 'python email test'
  10. smtpserver = 'smtp.163.com'
  11. username = '***'
  12. password = '***'
  13. msgRoot = MIMEMultipart('related') 
  14. msgRoot['Subject'] = 'test message'
  15. msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8') 
  16. msgRoot.attach(msgText) 
  17. fp = open('h:\\python\\1.jpg', 'rb') 
  18. msgImage = MIMEImage(fp.read()) 
  19. fp.close() 
  20. msgImage.add_header('Content-ID', '<image1>') 
  21. msgRoot.attach(msgImage) 
  22. smtp = smtplib.SMTP() 
  23. smtp.connect('smtp.163.com') 
  24. smtp.login(username, password) 
  25. smtp.sendmail(sender, receiver, msgRoot.as_string()) 
  26. smtp.quit() 

带附件的邮件

[python] view plaincopy

  1. #!/usr/bin/env python3
  2. #coding: utf-8
  3. import smtplib 
  4. from email.mime.multipart import MIMEMultipart 
  5. from email.mime.text import MIMEText 
  6. from email.mime.image import MIMEImage 
  7. sender = '***'
  8. receiver = '***'
  9. subject = 'python email test'
  10. smtpserver = 'smtp.163.com'
  11. username = '***'
  12. password = '***'
  13. msgRoot = MIMEMultipart('related') 
  14. msgRoot['Subject'] = 'test message'
  15. #构造附件
  16. att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8') 
  17. att["Content-Type"] = 'application/octet-stream'
  18. att["Content-Disposition"] = 'attachment; filename="1.jpg"'
  19. msgRoot.attach(att) 
  20. smtp = smtplib.SMTP() 
  21. smtp.connect('smtp.163.com') 
  22. smtp.login(username, password) 
  23. smtp.sendmail(sender, receiver, msgRoot.as_string()) 
  24. smtp.quit() 

群邮件

[python] view plaincopy

  1. #!/usr/bin/env python3
  2. #coding: utf-8
  3. import smtplib 
  4. from email.mime.text import MIMEText 
  5. sender = '***'
  6. receiver = ['***','****',……] 
  7. subject = 'python email test'
  8. smtpserver = 'smtp.163.com'
  9. username = '***'
  10. password = '***'
  11. msg = MIMEText('你好','plain','utf-8') 
  12. msg['Subject'] = subject 
  13. smtp = smtplib.SMTP() 
  14. smtp.connect('smtp.163.com') 
  15. smtp.login(username, password) 
  16. smtp.sendmail(sender, receiver, msg.as_string()) 
  17. smtp.quit() 

各种元素都包含的邮件

[python] view plaincopy

  1. #!/usr/bin/env python3
  2. #coding: utf-8
  3. import smtplib 
  4. from email.mime.multipart import MIMEMultipart 
  5. from email.mime.text import MIMEText 
  6. from email.mime.image import MIMEImage 
  7. sender = '***'
  8. receiver = '***'
  9. subject = 'python email test'
  10. smtpserver = 'smtp.163.com'
  11. username = '***'
  12. password = '***'
  13. # Create message container - the correct MIME type is multipart/alternative.
  14. msg = MIMEMultipart('alternative') 
  15. msg['Subject'] = "Link"
  16. # Create the body of the message (a plain-text and an HTML version).
  17. text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
  18. html = """\
  19. <html>
  20.   <head></head>
  21.   <body>
  22.     <p>Hi!<br>
  23.        How are you?<br>
  24.        Here is the <a href="http://www.python.org">link</a> you wanted.
  25.     </p>
  26.   </body>
  27. </html>
  28. """
  29. # Record the MIME types of both parts - text/plain and text/html.
  30. part1 = MIMEText(text, 'plain') 
  31. part2 = MIMEText(html, 'html') 
  32. # Attach parts into message container.
  33. # According to RFC 2046, the last part of a multipart message, in this case
  34. # the HTML message, is best and preferred.
  35. msg.attach(part1) 
  36. msg.attach(part2) 
  37. #构造附件
  38. att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8') 
  39. att["Content-Type"] = 'application/octet-stream'
  40. att["Content-Disposition"] = 'attachment; filename="1.jpg"'
  41. msg.attach(att) 
  42. smtp = smtplib.SMTP() 
  43. smtp.connect('smtp.163.com') 
  44. smtp.login(username, password) 
  45. smtp.sendmail(sender, receiver, msg.as_string()) 
  46. smtp.quit() 

基于SSL的邮件

[python] view plaincopy

  1. #!/usr/bin/env python3
  2. #coding: utf-8
  3. import smtplib 
  4. from email.mime.text import MIMEText 
  5. from email.header import Header 
  6. sender = '***'
  7. receiver = '***'
  8. subject = 'python email test'
  9. smtpserver = 'smtp.163.com'
  10. username = '***'
  11. password = '***'
  12. msg = MIMEText('你好','plain','utf-8')#中文需参数‘utf-8’,单字节字符不需要
  13. msg['Subject'] = Header(subject, 'utf-8') 
  14. smtp = smtplib.SMTP() 
  15. smtp.connect('smtp.163.com') 
  16. smtp.ehlo() 
  17. smtp.starttls() 
  18. smtp.ehlo() 
  19. smtp.set_debuglevel(1) 
  20. smtp.login(username, password) 
  21. smtp.sendmail(sender, receiver, msg.as_string()) 
  22. smtp.quit() 
posted @ 2012-11-14 11:45  adodo1  Views(138)  Comments(0Edit  收藏  举报