文件形式的邮件

[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. Linux命令应用大词典-第4章 目录和文件操作

    4.1 pwd:显示(打印)当前工作目录路径 4.2 cd:更改工作目录路径 4.3 ls: 列出目录和文件信息: 4.4 dir:列出目录或文件信息: 4.5 dirs:显示目录列表: 4.6 to ...

  2. 小组ITalk网站开发中使用到的一些技巧

    ----->Display属性和Visibility属性:一个清除内容和框体,另一个只清除内容而保留窗体: $('#abc').css({ 'font-size' : '12px', '-web ...

  3. java并发总览

  4. tomcat下载、安装

    下载 官网地址:https://tomcat.apache.org/download-80.cgi 安装 直接安装即可.安装完毕后Tomcat的目录结构如下: bin:脚本目录 ​ 启动脚本:star ...

  5. leetcode个人题解——two sum

    这是leetcode第一题,通过较为简单. 第一题用来测试的,用的c,直接暴力法过, /** * Note: The returned array must be malloced, assume c ...

  6. ssh连接失败, 记下来原因和解决方案

    mac下使用secureCRT发现连接不了虚拟机上的linux 运行 ps -e | grep ssh,查看是否有sshd进程 如果没有,说明server没启动,通过 /etc/init.d/sshd ...

  7. 欢迎来怼-Alpha周(2017年10月19)贡献分配规则和分配结果

    .从alpha周(2017年10月19日开始的2周)开始,提高贡献分比重. 贡献分 : 团队分 = 1 : 5 教师会在核算每位同学总分时按比例乘以系数. 每位同学带入团队贡献分10分,如果团队一共7 ...

  8. php5.4以上运行yii框架出现问题的解决方法

    Ubuntu Server 下安装 Mcrypt Php Extension http://blog.archean.me/2013/10/22/install-mcrypt-php-extensio ...

  9. Android中的回调Callback

    回调就是外部设置一个方法给一个对象, 这个对象可以执行外部设置的方法, 通常这个方法是定义在接口中的抽象方法, 外部设置的时候直接设置这个接口对象即可. 例如给安卓添加按钮点击事件, 我们创建了OnC ...

  10. JavaScript初探系列之数组的基本操作

    在程序语言中数组的重要性不言而喻,JavaScript中数组也是最常使用的对象之一,数组是值的有序集合,由于弱类型的原因,JavaScript中数组十分灵活.强大,不像是Java等强类型高级语言数组只 ...