文件形式的邮件

  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('你好','text','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形式的邮件

  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邮件

  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.p_w_picpath 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 p_w_picpath.<br><img src="cid:p_w_picpath1"><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', '<p_w_picpath1>')

  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()

带附件的邮件

  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.p_w_picpath 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"] = 'p_w_upload; 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()

群邮件

  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('你好','text','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()

各种元素都包含的邮件

  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.p_w_picpath 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"] = 'p_w_upload; 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的邮件

  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('你好','text','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()

    文件形式的邮件

    HTML形式的邮件

    带图片的HTML邮件

    带附件的邮件

    群邮件

    各种元素都包含的邮件

    基于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. msg = MIMEText('你好','text','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()

    23. #!/usr/bin/env python3

    24. #coding: utf-8

    25. import smtplib

    26. from email.mime.multipart import MIMEMultipart

    27. from email.mime.text import MIMEText

    28. from email.mime.p_w_picpath import MIMEImage

    29. sender = '***'

    30. receiver = '***'

    31. subject = 'python email test'

    32. smtpserver = 'smtp.163.com'

    33. username = '***'

    34. password = '***'

    35. # Create message container - the correct MIME type is multipart/alternative.

    36. msg = MIMEMultipart('alternative')

    37. msg['Subject'] = "Link"

    38. # Create the body of the message (a plain-text and an HTML version).

    39. text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"

    40. html = """\

    41. <html>

    42. <head></head>

    43. <body>

    44. <p>Hi!<br>

    45. How are you?<br>

    46. Here is the <a href="http://www.python.org">link</a> you wanted.

    47. </p>

    48. </body>

    49. </html>

    50. """

    51. # Record the MIME types of both parts - text/plain and text/html.

    52. part1 = MIMEText(text, 'plain')

    53. part2 = MIMEText(html, 'html')

    54. # Attach parts into message container.

    55. # According to RFC 2046, the last part of a multipart message, in this case

    56. # the HTML message, is best and preferred.

    57. msg.attach(part1)

    58. msg.attach(part2)

    59. #构造附件

    60. att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')

    61. att["Content-Type"] = 'application/octet-stream'

    62. att["Content-Disposition"] = 'p_w_upload; filename="1.jpg"'

    63. msg.attach(att)

    64. smtp = smtplib.SMTP()

    65. smtp.connect('smtp.163.com')

    66. smtp.login(username, password)

    67. smtp.sendmail(sender, receiver, msg.as_string())

    68. smtp.quit()

    69. #!/usr/bin/env python3

    70. #coding: utf-8

    71. import smtplib

    72. from email.mime.text import MIMEText

    73. sender = '***'

    74. receiver = ['***','****',……]

    75. subject = 'python email test'

    76. smtpserver = 'smtp.163.com'

    77. username = '***'

    78. password = '***'

    79. msg = MIMEText('你好','text','utf-8')

    80. msg['Subject'] = subject

    81. smtp = smtplib.SMTP()

    82. smtp.connect('smtp.163.com')

    83. smtp.login(username, password)

    84. smtp.sendmail(sender, receiver, msg.as_string())

    85. 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.p_w_picpath 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"] = 'p_w_upload; 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()

    25. #!/usr/bin/env python3

    26. #coding: utf-8

    27. import smtplib

    28. from email.mime.multipart import MIMEMultipart

    29. from email.mime.text import MIMEText

    30. from email.mime.p_w_picpath import MIMEImage

    31. sender = '***'

    32. receiver = '***'

    33. subject = 'python email test'

    34. smtpserver = 'smtp.163.com'

    35. username = '***'

    36. password = '***'

    37. msgRoot = MIMEMultipart('related')

    38. msgRoot['Subject'] = 'test message'

    39. msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an p_w_picpath.<br><img src="cid:p_w_picpath1"><br>good!','html','utf-8')

    40. msgRoot.attach(msgText)

    41. fp = open('h:\\python\\1.jpg', 'rb')

    42. msgImage = MIMEImage(fp.read())

    43. fp.close()

    44. msgImage.add_header('Content-ID', '<p_w_picpath1>')

    45. msgRoot.attach(msgImage)

    46. smtp = smtplib.SMTP()

    47. smtp.connect('smtp.163.com')

    48. smtp.login(username, password)

    49. smtp.sendmail(sender, receiver, msgRoot.as_string())

    50. smtp.quit()

    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()

    18. #!/usr/bin/env python3

    19. #coding: utf-8

    20. import smtplib

    21. from email.mime.text import MIMEText

    22. from email.header import Header

    23. sender = '***'

    24. receiver = '***'

    25. subject = 'python email test'

    26. smtpserver = 'smtp.163.com'

    27. username = '***'

    28. password = '***'

    29. msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要

    30. msg['Subject'] = Header(subject, 'utf-8')

    31. smtp = smtplib.SMTP()

    32. smtp.connect('smtp.163.com')

    33. smtp.login(username, password)

    34. smtp.sendmail(sender, receiver, msg.as_string())

    35. smtp.quit()

转载于:https://blog.51cto.com/6226001001/1693201

python 发送邮件实例相关推荐

  1. Python发送邮件实例

    在实际开发当中,有时候就需要用到Python发送一些代码构造的邮件信息,最近刚好有接触到这一块,就顺便总结一下. 1.发html.plain格式邮件,不带附件 # -*- coding: UTF-8 ...

  2. html发照片的文本实例,python发送邮件的实例代码(支持html、图片、附件) -电脑资料...

    第一段代码: 复制代码代码如下: #!/usr/bin/python # -*- coding: utf-8 -*- import email import mimetypes from email. ...

  3. Python编程实例-Python发送邮件

    Python发送邮件 Python 带有内置的 smtplib 模块,用于使用简单邮件传输协议 (SMTP) 发送电子邮件. smtplib 对 SMTP 使用 RFC 821 协议. 本文中的示例将 ...

  4. python邮件发送哪个好_最全总结!聊聊 Python 发送邮件的几种方式

    1. 前言 邮件,作为最正式规范的沟通方式,在日常办公过程中经常被用到 我们都知道 Python内置了对 SMTP 的支持,可以发送纯文本.富文本.HTML 等格式的邮件 本文将聊聊利用 Python ...

  5. python发送邮件及附件

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

  6. python 发送邮件connect none_Python发送邮件功能示例【使用QQ邮箱】

    本文实例讲述了Python发送邮件功能.分享给大家供大家参考,具体如下: 这里以QQ邮箱为例说明 登录邮箱点账号 开启smtp 开启时会要求你发送一条短信,发送完成后点已发送. 就有授权码了 代码如下 ...

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

    1. 前言 邮件,作为最正式规范的沟通方式,在日常办公过程中经常被用到 很多人学习python,不知道从何学起. 很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手. 很多已经做案例 ...

  8. python发送邮件(一)

    python发送邮件(一) 最近设计了一个小的应用程序,主要是根据文件中邮件地址发送一份excel中内容,并且在接受方收到邮件都是以网页的格式呈现的. 下面主要是对python发送邮件涉及到的部分知识 ...

  9. python自动发邮件富文本_Python自动化测试发送邮件太麻烦?!一起聊一聊 Python 发送邮件的3种方式...

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

最新文章

  1. 世界第三大浏览器正在消亡
  2. android添加imageview,android – 以编程方式将ImageView添加到Layout
  3. 如何利用Winsock控件编写自己的Internet程序
  4. 背完这444句,你的口语绝对不成问题了
  5. 使用MEF构建可扩展的Silverlight应用
  6. 办公技巧:Excel下拉菜单小技巧,赶紧学一下!
  7. 计算机基础理论知识梳理篇(二):目态(用户态)、管态(内核态)
  8. 全球1/10女性受到盆腔脏器脱垂困扰
  9. 最全面的理解 | 工业互联网的前世今生
  10. Windows Server AppFabric 安装文档
  11. mysql的会话变量,全局变量,状态信息
  12. gbq可以算出土建量吗_广联达土建算量软件必备操作指南
  13. NET Framework 精简版可以获得的常用功能
  14. Mac命令行出现-bash: command not found的解决办法
  15. Xcode8写代码闪退
  16. java通过itextpdf实现pdf文件加水印
  17. 计算机控制键盘,键盘装置及其计算机控制系统的制作方法
  18. 微软洗牌低代码开发市场,引发软件产业生态变局
  19. 学习笔记5--高精地图技术
  20. flex:0 flex:1 flex:auto flex:none之间的区别

热门文章

  1. sqlmap绕过d盾_WEBSHELL免杀绕过WAF思路amp;方法(一)
  2. Python 中读取和保存图像方法汇总及其区别
  3. systemd进程管理工具实战教程
  4. 华为交换机ssh思科交换机_思科交换机交换机中ip、mac地址绑定
  5. java 数字三角形_数字三角形 Number Triangles(java的MLE解决办法)
  6. 【Java代码实现】递归两大经典问题-----“汉诺塔问题” 与 “青蛙跳台阶问题” 讲解
  7. 习题4-1 求奇数和 (15 分)
  8. 【数学专题】 筛质数、分解质因数和快速幂
  9. 中石油计算机职称考试题,2019年职称英语考试中石油历年真题及答案
  10. A - Expanding Rods POJ - 1905(二分)