文件形式的邮件

[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. Uncaught Error: code length overflow. (1604>1056)

    解决方法来源~~~https://blog.csdn.net/arrowzz/article/details/80656510 二维码生成时,如果长度太长会有异常: Uncaught Error: c ...

  2. Leetcode-跳跃游戏

    跳跃游戏     给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] ...

  3. 拥抱移动端,jQueryui触控设备兼容插件

    http://touchpunch.furf.com/ ps:要FQ. jQuery UI Touch Punch Touch Event Support for jQuery UI Tested o ...

  4. [转]bashrc与profile区别

    作者:KornLee 2005-02-03 15:49:57 来自:Linux先生 /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/pro ...

  5. 告别加载dll 出错开机加载项大揭秘

    提到开机加载(load)项,大家不要以为就是系统启动(run)项.最简单的例子是,杀毒软件或者用户手动删除病毒文件后,注册表中的自动加载信息仍在,登陆系统时就会提示"加载*dll出错,系统找 ...

  6. [转载]启动tomcat时,一直卡在Deploying web application directory这块的解决方案

    转载:https://www.cnblogs.com/mycifeng/p/6972446.html 本来今天正常往服务器上扔一个tomcat 部署一个项目的, 最后再启动tomcat 的时候 发现项 ...

  7. C语言链接数据库

    一.解释一下函数功能和用法 1.mysql_real_connect 函数原型:MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, co ...

  8. HDU 4302 Holedox Eating (线段树模拟)

    题意:一个老鼠在一条长度为L的直线上跑,吃蛋糕,老鼠只能沿直线移动.开始时没有蛋糕,老鼠的初始位置是0. 有两个操作,0 x 代表在位置x添加一个蛋糕: 1 代表老鼠想吃蛋糕.老鼠每次都会选择离自己最 ...

  9. 自测之Lesson12:信号量

    题目:创建一个包含5个信号量的信号集. 完成代码: #include <stdio.h> #include <sys/ipc.h> #include <sys/sem.h ...

  10. c++ 第五次作业(计算器第三步)

    第五次作业 (计算器第三步) 项目源文件地址:calculator 本次作业改进情况 加入多种读入选择 正常输出答案 -a 选项,输出表达式以及值 -f 选项,从指定文件读入,并把答案输出到指定文件 ...