文件形式的邮件

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

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

各种元素都包含的邮件

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

    文件形式的邮件

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

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

    各种元素都包含的邮件

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

转载:http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343463.html

python邮件服务的更多相关文章

  1. python邮件服务-yagmail

      下载安装 yagmail import yagmail #链接邮箱服务器 #此处的password是授权码 yag= yagmail.SMTP( user="843092012@qq.c ...

  2. 免费SMTP邮件服务:Mandrill,Sendgrid,Mailjet,Postmarkapp,MailChimp

    免费的SMTP发邮件服务平常几乎都没有怎么关注,Wordpress发邮件几乎用普通的邮箱的SMTP服务就可以完成任务,但是自从用了Discourse.Ghost博客后,发现找到一个合适的.免费配额大的 ...

  3. centos 7 keepalived故障邮件通知实战(附Python邮件发送脚本)

    centos 7 keepalived故障邮件通知实战(附Python邮件发送脚本) #####################     sendmail.py  begin     ######## ...

  4. (转) 关于Oracle EBS邮件服务无法使用的报错

    来源http://blog.itpub.net/23850820/viewspace-1060596/ 也可以检查如下网站:http://blog.sina.com.cn/s/blog_5b021b4 ...

  5. <邮件服务postfix+mysql>MAIL第二篇

    环境:本服务是建立在第一篇的基础之上的,最好搭建好第一篇 玩此服务的前提是你的系统装好了msql和postfix服务. Postfix+mysql主要是把邮件服务的发与mysql结合使用.当然mysq ...

  6. Java邮件服务学习之四:邮箱服务客户端Spring Mail

    一.Spring Mail API Spring邮件抽象层的主要包为org.springframework.mail,Spring提供的邮件发送不仅支持简单邮件的发送.添加附件. 1.邮件发送的核心接 ...

  7. Java邮件服务学习之一:邮件服务概述

    java可以提供邮件服务:一般理解的邮件服务就是可以发送和接收邮件的客户端,另外就是使用java编写邮件服务端:两者区别在于客户端只负责给终端客户收发邮件,就相当于小区楼下的那一排排的铁皮邮箱盒,而邮 ...

  8. Python队列服务 Python RQ Functions from the __main__ module cannot be processed by workers.

    在使用Python队列服务 Python RQ 时候的报错: Functions from the __main__ module cannot be processed by workers. 原因 ...

  9. 戏说WSGI(Python Web服务网关接口)--[转载]

    戏说WSGI(Python Web服务网关接口) 当你在Python的世界中冒险,突然遭遇一只Web怪兽,你会选择什么武器对付它?在兵器谱上,下列兵器可谓名列前茅: Zope,厚重的长枪.较早出现的武 ...

随机推荐

  1. 分布式部署Apache-Jmeter粗略流程

    注意事项 Windows版和Mac版Jmeter可互相通信 确认被部署的机器安装有JDK并已配置好环境变量 Controller安装 1. 安装Jmeter,监视插件JMeterPlugins-Sta ...

  2. (C#)工厂方法模式

    1.工厂方法模式 第一了一个用于创建对象的接口,让子类自己决定实例化哪一个类.工厂方法使一个类的实例化延迟到其子类. *工厂方法模式即克服了简单工厂模式违反开放-封闭原则的缺点,又保留了封装对象创建过 ...

  3. 如何在线测试Exchange的速度

    最新碰到了客户需要比较国内版和国际版的Office365的速度问题,微软提供在线工具测试 这里以Exchange 测试为例子,请参考. PS Onenote贴过来只能至图片,各位看官只能将就了 这里有

  4. 动画效果 ObjectAnimator

    学习了一下动画效果的使用,做一下笔记 ImageView imageView = findViewById(R.id.imageView); ObjectAnimator.ofFloat(imageV ...

  5. 20172330 2017-2018-1 《Java程序设计》第三周学习总结

    20172330 2017-2018-1 <Java程序设计>第三周学习总结 教材学习内容总结 这一章的主要内容是关于类与对象,通过对String类,Random类,Math类等一系列道德 ...

  6. 【IdentityServer4文档】- 打包和构建

    打包和构建 IdentityServer 由多个 nuget 软件包组成的. IdentityServer4 nuget | github 包含 IdentityServer 核心对象模型,服务和中间 ...

  7. 内部网关协议RIP 路由选择算法(距离向量)

    RIP是一种基于距离向量的路由选择协议 RIP的距离就是指的跳数,没经过一个路由,就是一跳,RIP允许一跳路径最多经过15个路由器,所以16个的话就相当于不可以到达了 RIP协议的特点: 1:仅和相邻 ...

  8. iOS开发allocWithZone介绍

    首先我们知道,我们需要保证单例类只有一个唯一的实例,而平时我们在初始化一个对象的时候, [[Class alloc] init],其实是做了两件事. alloc 给对象分配内存空间,init是对对象的 ...

  9. 软工网络15团队作业——Alpha阶段敏捷冲刺 DAY1

    Alpha阶段敏捷冲刺 DAY1 1.各个成员在 Alpha 阶段认领的任务 姓名 在Alpha阶段所认领的任务 陈龙 题目生成类的编写,随机生成合理题目的算法编写 郑佳明 答案计算类的编写,对随机生 ...

  10. 【Docker 教程】- Docker 架构

    1.Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器. 2.Docker 容器通过 Docker 镜像来创建. 3.容器与镜像的关系类似于面向对象编程 ...