文件形式的邮件

  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.  
  7. sender = '***'
  8. receiver = '***'
  9. subject = 'python email test'
  10. smtpserver = 'smtp.163.com'
  11. username = '***'
  12. password = '***'
  13.  
  14. msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要
  15. msg['Subject'] = Header(subject, 'utf-8')
  16.  
  17. smtp = smtplib.SMTP()
  18. smtp.connect('smtp.163.com')
  19. smtp.login(username, password)
  20. smtp.sendmail(sender, receiver, msg.as_string())
  21. smtp.quit()

HTML形式的邮件

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

带图片的HTML邮件

  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.  
  8. sender = '***'
  9. receiver = '***'
  10. subject = 'python email test'
  11. smtpserver = 'smtp.163.com'
  12. username = '***'
  13. password = '***'
  14.  
  15. msgRoot = MIMEMultipart('related')
  16. msgRoot['Subject'] = 'test message'
  17.  
  18. msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8')
  19. msgRoot.attach(msgText)
  20.  
  21. fp = open('h:\\python\\1.jpg', 'rb')
  22. msgImage = MIMEImage(fp.read())
  23. fp.close()
  24.  
  25. msgImage.add_header('Content-ID', '<image1>')
  26. msgRoot.attach(msgImage)
  27.  
  28. smtp = smtplib.SMTP()
  29. smtp.connect('smtp.163.com')
  30. smtp.login(username, password)
  31. smtp.sendmail(sender, receiver, msgRoot.as_string())
  32. smtp.quit()

带附件的邮件

  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.  
  8. sender = '***'
  9. receiver = '***'
  10. subject = 'python email test'
  11. smtpserver = 'smtp.163.com'
  12. username = '***'
  13. password = '***'
  14.  
  15. msgRoot = MIMEMultipart('related')
  16. msgRoot['Subject'] = 'test message'
  17.  
  18. #构造附件
  19. att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
  20. att["Content-Type"] = 'application/octet-stream'
  21. att["Content-Disposition"] = 'attachment; filename="1.jpg"'
  22. msgRoot.attach(att)
  23.  
  24. smtp = smtplib.SMTP()
  25. smtp.connect('smtp.163.com')
  26. smtp.login(username, password)
  27. smtp.sendmail(sender, receiver, msgRoot.as_string())
  28. smtp.quit()

群邮件

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

各种元素都包含的邮件

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

基于SSL的邮件

  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.  
  13. msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要
  14. msg['Subject'] = Header(subject, 'utf-8')
  15.  
  16. smtp = smtplib.SMTP()
  17. smtp.connect('smtp.163.com')
  18. smtp.ehlo()
  19. smtp.starttls()
  20. smtp.ehlo()
  21. smtp.set_debuglevel(1)
  22. smtp.login(username, password)
  23. smtp.sendmail(sender, receiver, msg.as_string())
  24. smtp.quit()

文件形式的邮件

  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.  
  7. sender = '***'
  8. receiver = '***'
  9. subject = 'python email test'
  10. smtpserver = 'smtp.163.com'
  11. username = '***'
  12. password = '***'
  13.  
  14. msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要
  15. msg['Subject'] = Header(subject, 'utf-8')
  16.  
  17. smtp = smtplib.SMTP()
  18. smtp.connect('smtp.163.com')
  19. smtp.login(username, password)
  20. smtp.sendmail(sender, receiver, msg.as_string())
  21. smtp.quit()

HTML形式的邮件

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

带图片的HTML邮件

  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.  
  8. sender = '***'
  9. receiver = '***'
  10. subject = 'python email test'
  11. smtpserver = 'smtp.163.com'
  12. username = '***'
  13. password = '***'
  14.  
  15. msgRoot = MIMEMultipart('related')
  16. msgRoot['Subject'] = 'test message'
  17.  
  18. msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8')
  19. msgRoot.attach(msgText)
  20.  
  21. fp = open('h:\\python\\1.jpg', 'rb')
  22. msgImage = MIMEImage(fp.read())
  23. fp.close()
  24.  
  25. msgImage.add_header('Content-ID', '<image1>')
  26. msgRoot.attach(msgImage)
  27.  
  28. smtp = smtplib.SMTP()
  29. smtp.connect('smtp.163.com')
  30. smtp.login(username, password)
  31. smtp.sendmail(sender, receiver, msgRoot.as_string())
  32. smtp.quit()

带附件的邮件

  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.  
  8. sender = '***'
  9. receiver = '***'
  10. subject = 'python email test'
  11. smtpserver = 'smtp.163.com'
  12. username = '***'
  13. password = '***'
  14.  
  15. msgRoot = MIMEMultipart('related')
  16. msgRoot['Subject'] = 'test message'
  17.  
  18. #构造附件
  19. att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
  20. att["Content-Type"] = 'application/octet-stream'
  21. att["Content-Disposition"] = 'attachment; filename="1.jpg"'
  22. msgRoot.attach(att)
  23.  
  24. smtp = smtplib.SMTP()
  25. smtp.connect('smtp.163.com')
  26. smtp.login(username, password)
  27. smtp.sendmail(sender, receiver, msgRoot.as_string())
  28. smtp.quit()

群邮件

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

各种元素都包含的邮件

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

基于SSL的邮件

  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.  
  13. msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要
  14. msg['Subject'] = Header(subject, 'utf-8')
  15.  
  16. smtp = smtplib.SMTP()
  17. smtp.connect('smtp.163.com')
  18. smtp.ehlo()
  19. smtp.starttls()
  20. smtp.ehlo()
  21. smtp.set_debuglevel(1)
  22. smtp.login(username, password)
  23. smtp.sendmail(sender, receiver, msg.as_string())
  24. smtp.quit()

<转>python 发送邮件实例的更多相关文章

  1. python 发送邮件实例

    留言板回复作者邮件提醒 -----------2016-5-11 15:03:58-- source:python发送邮件实例

  2. 【转载】python发送邮件实例

    本文转自:http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343463.html 这几天要用python发送邮件,上网找到这篇文章感觉 ...

  3. python发送邮件实例1

    文件形式的邮件 #!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.text import MIMEText fr ...

  4. python发送邮件及附件

    今天给大伙说说python发送邮件,官方的多余的话自己去百度好了,还有一大堆文档说实话不到万不得已的时候一般人都不会去看,回归主题: 本人是mac如果没有按照依赖模块的请按照下面的截图安装 导入模块如 ...

  5. Python发送邮件(最全)

    简单邮件传输协议(SMTP)是一种协议,用于在邮件服务器之间发送电子邮件和路由电子邮件. Python提供smtplib模块,该模块定义了一个SMTP客户端会话对象,可用于使用SMTP或ESMTP侦听 ...

  6. 最全总结!聊聊 Python 发送邮件的几种方式

    1. 前言 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人,我给大 ...

  7. Python自动化测试发送邮件太麻烦?!一起聊一聊 Python 发送邮件的3种方式

    1. 前言 发送邮件,我们在平时工作中经用到,做为测试人员,在自动化测试中用的也比较多,需要发送邮件给某领导 SMTP是Python默认的邮件模块,可以发送纯文本.富文本.HTML 等格式的邮件 今天 ...

  8. python发送邮件

    python发送邮件(无附件) ======================================================= #!/usr/bin/env python#coding ...

  9. python基础——实例属性和类属性

    python基础——实例属性和类属性 由于Python是动态语言,根据类创建的实例可以任意绑定属性. 给实例绑定属性的方法是通过实例变量,或者通过self变量: class Student(objec ...

随机推荐

  1. 清北学堂模拟赛d7t6 拯救世界

    分析:如果题目中没有环的话就是一道裸的最长路的题目,一旦有环每个城市就会被救多次火了.把有向有环图变成有向无环图只需要tarjan一边就可以了. #include <bits/stdc++.h& ...

  2. 手工MAVEN建立WEBAPP项目并打包部署

    参考URL: http://my.oschina.net/zimingforever/blog/266028 最简单的东东,可以就两条命令: 建立目录及POM.XML: mvn archetype:g ...

  3. HDU 5393

    [background] 保研的事终于告一段落了,之后去北京折腾了一段时间,本以为会在那里实习一个月,谁知道刚去ICT,心中就各种反感,可能是因为LP的态度吧,否则我可能会留在那里读研也说不定.花了两 ...

  4. [Tools] VS Code Tips

    Inside one file, you can freely mark the number 1-9: -] And jump to Number of bookmark: cmd + [-] It ...

  5. linux c编程訪问数据库

    源代码例如以下: #include <stdio.h> #include <stdlib.h> #include <mysql/mysql.h> int main( ...

  6. QQ是怎样实现好友桌面快捷方式的?

    QQ是怎样实现好友桌面快捷方式的? 不知道什么时候,QQ推出了好友桌面快捷方式.方便用户和最常保持联系的好友一键联系.核心功能一:若QQ启动了.则双击快捷方式直接打开好友聊天界面:核心功能二:若QQ未 ...

  7. Spring为了简化java开发採用的四种策略

    以下是<Spring in action>中的总结性语言,记录下来,作为研究源码的主要线索. 1.採用轻量级的pojo.最小侵入式编程. 2.依赖注入(DI)和面向接口编程实现松耦合. 3 ...

  8. oc33--构造方法2

    // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property int age; ...

  9. word2vec和word embedding有什么区别?

    word2vec和word embedding有什么区别? 我知道这两个都能将词向量化,但有什么区别?这两个术语的中文是什么? from: https://www.zhihu.com/question ...

  10. Codeforces--626B--Cards(模拟)

     Cards Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Sta ...