文件形式的邮件

[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. CentOS安装nmon

    nmon官网: http://nmon.sourceforge.net/pmwiki.php?n=Main.HomePage 下载nmon16e_mpginc.tar.gz到本地并上传到服务器 tar ...

  2. 【JAVA】关于java中 类.class.getResource("/").getPath()获取路径有空格的问题

    写了一个web工程,在本地测试正确,但是部署到服务器上就出现错误.原因是读取不到配置文件. 后来从打印出来的文件路径中发现是用Java的class.getResource("/") ...

  3. ubuntu 执行Python脚本出现: /usr/bin/env: ‘python\r’: No such file or directory

    原因: #!/usr/bin/env python 在ubuntu会变成 #!/usr/bin/env python\r 而\r 会被shell 当成参数 所以出现:  /usr/bin/env: ‘ ...

  4. 爬虫2.1-scrapy框架-两种爬虫对比

    目录 scrapy框架-两种爬虫对比和大概流程 1. 传统spider爬虫 2. crawl型爬虫 3. 循环页面请求 4. scrapy框架爬虫的大致流程 scrapy框架-两种爬虫对比和大概流程 ...

  5. JavaScriptSerializer的实现-常用JsonHelper类

    最近开始自己写自己的项目了,终于鼓起勇气迈出了自己认为的这一大步! 先来通用的helper类和大家分享一下 ,第一个是Object转为json序列的类,这个网上有很多,但我实践了一下大部分都不能用的, ...

  6. windows store无法登陆的问题解决方案

    Windows应用商店或商店Apps无法打开或闪退的可选方法 (仅用于10565之前的Windows 10版本) 右键点击任务栏,选择"属性",切换到"导航"选 ...

  7. Python3 小工具-MAC泛洪

    from scapy.all import * import optparse def attack(interface): pkt=Ether(src=RandMAC(),dst=RandMAC() ...

  8. Python3 标准库:calendar,time

    1.calendar import calendar print(calendar.month(2008,8)) #某个月 print(calendar.calendar(2008)) #某年 pri ...

  9. [leetcode-784-Letter Case Permutation]

    Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...

  10. 将HTML页面页脚固定在页面底部(多种方法实现)

    当一个HTML页面中含有较少的内容时,Web页面的footer部分随着飘上来,处在页面的半腰中间,给视觉效果带来极大的影响,接下来为大家介绍下如何将页脚固定在页面底部,感兴趣的朋友可以了解下 作为一个 ...