特别留意群邮件方式,这是工作中用得多的。

附件,HTML,图片,都需要的。

文件形式的邮件

[python]  view plain copy 

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.
7.sender = '***'
8.receiver = '***'
9.subject = 'python email test'
10.smtpserver = 'smtp.163.com'
11.username = '***'
12.password = '***'
13.
14.msg = MIMEText('你好','plain','utf-8')#中文需参数‘utf-8’,单字节字符不需要
15.msg['Subject'] = Header(subject, 'utf-8')
16.
17.smtp = smtplib.SMTP()
18.smtp.connect('smtp.163.com')
19.smtp.login(username, password)
20.smtp.sendmail(sender, receiver, msg.as_string())
21.smtp.quit()  

HTML形式的邮件

[python]  view plain copy 

1.#!/usr/bin/env python3
2.#coding: utf-8
3.import smtplib
4.from email.mime.text import MIMEText
5.
6.sender = '***'
7.receiver = '***'
8.subject = 'python email test'
9.smtpserver = 'smtp.163.com'
10.username = '***'
11.password = '***'
12.
13.msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8')
14.
15.msg['Subject'] = subject
16.
17.smtp = smtplib.SMTP()
18.smtp.connect('smtp.163.com')
19.smtp.login(username, password)
20.smtp.sendmail(sender, receiver, msg.as_string())
21.smtp.quit()  

带图片的HTML邮件

[python]  view plain copy 

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.
8.sender = '***'
9.receiver = '***'
10.subject = 'python email test'
11.smtpserver = 'smtp.163.com'
12.username = '***'
13.password = '***'
14.
15.msgRoot = MIMEMultipart('related')
16.msgRoot['Subject'] = 'test message'
17.
18.msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8')
19.msgRoot.attach(msgText)
20.
21.fp = open('h:\\python\\1.jpg', 'rb')
22.msgImage = MIMEImage(fp.read())
23.fp.close()
24.
25.msgImage.add_header('Content-ID', '<image1>')
26.msgRoot.attach(msgImage)
27.
28.smtp = smtplib.SMTP()
29.smtp.connect('smtp.163.com')
30.smtp.login(username, password)
31.smtp.sendmail(sender, receiver, msgRoot.as_string())
32.smtp.quit()
带附件的邮件 

[python]  view plain copy 

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.
8.sender = '***'
9.receiver = '***'
10.subject = 'python email test'
11.smtpserver = 'smtp.163.com'
12.username = '***'
13.password = '***'
14.
15.msgRoot = MIMEMultipart('related')
16.msgRoot['Subject'] = 'test message'
17.
18.#构造附件
19.att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
20.att["Content-Type"] = 'application/octet-stream'
21.att["Content-Disposition"] = 'attachment; filename="1.jpg"'
22.msgRoot.attach(att)
23.
24.smtp = smtplib.SMTP()
25.smtp.connect('smtp.163.com')
26.smtp.login(username, password)
27.smtp.sendmail(sender, receiver, msgRoot.as_string())
28.smtp.quit()  

群邮件

[python]  view plain copy 

1.#!/usr/bin/env python3
2.#coding: utf-8
3.import smtplib
4.from email.mime.text import MIMEText
5.
6.sender = '***'
7.receiver = ['***','****',……]
8.subject = 'python email test'
9.smtpserver = 'smtp.163.com'
10.username = '***'
11.password = '***'
12.
13.msg = MIMEText('你好','plain','utf-8')
14.
15.msg['Subject'] = subject
16.
17.smtp = smtplib.SMTP()
18.smtp.connect('smtp.163.com')
19.smtp.login(username, password)
20.smtp.sendmail(sender, receiver, msg.as_string())
21.smtp.quit()  

各种元素都包含的邮件

[python]  view plain copy 

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.
8.sender = '***'
9.receiver = '***'
10.subject = 'python email test'
11.smtpserver = 'smtp.163.com'
12.username = '***'
13.password = '***'
14.
15.# Create message container - the correct MIME type is multipart/alternative.
16.msg = MIMEMultipart('alternative')
17.msg['Subject'] = "Link"
18.
19.# Create the body of the message (a plain-text and an HTML version).
20.text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
21.html = """\
22.<html>
23.  <head></head>
24.  <body>
25.    <p>Hi!<br>
26.       How are you?<br>
27.       Here is the <a href="http://www.python.org">link</a> you wanted.
28.    </p>
29.  </body>
30.</html>
31."""
32.
33.# Record the MIME types of both parts - text/plain and text/html.
34.part1 = MIMEText(text, 'plain')
35.part2 = MIMEText(html, 'html')
36.
37.# Attach parts into message container.
38.# According to RFC 2046, the last part of a multipart message, in this case
39.# the HTML message, is best and preferred.
40.msg.attach(part1)
41.msg.attach(part2)
42.#构造附件
43.att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')
44.att["Content-Type"] = 'application/octet-stream'
45.att["Content-Disposition"] = 'attachment; filename="1.jpg"'
46.msg.attach(att)
47.
48.smtp = smtplib.SMTP()
49.smtp.connect('smtp.163.com')
50.smtp.login(username, password)
51.smtp.sendmail(sender, receiver, msg.as_string())
52.smtp.quit()  

基于SSL的邮件

[python]  view plain copy 

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.
13.msg = MIMEText('你好','plain','utf-8')#中文需参数‘utf-8’,单字节字符不需要
14.msg['Subject'] = Header(subject, 'utf-8')
15.
16.smtp = smtplib.SMTP()
17.smtp.connect('smtp.163.com')
18.smtp.ehlo()
19.smtp.starttls()
20.smtp.ehlo()
21.smtp.set_debuglevel(1)
22.smtp.login(username, password)
23.smtp.sendmail(sender, receiver, msg.as_string())
24.smtp.quit()  

【转】python3 发邮件实例(包括:文本、html、图片、附件、SSL、群邮件)的更多相关文章

  1. python3 练手实例8 批量命名图片

    #coding:utf-8 import os import tkinter as tk from tkinter import filedialog root = tk.Tk() root.with ...

  2. 基于Java Mail 进行发送(带附件和压缩附件)的邮件

    刚进公司的training, 下面是要求: Self-study of Java Mail library:  http://www.oracle.com/technetwork/java/javam ...

  3. C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)

    我们知道,企业号主要是面向企业需求而生的,因此内部消息的交流显得非常重要,而且发送.回复消息数量应该很可观,对于大企业尤其如此,因此可以结合企业号实现内部消息的交流.企业号具有关注安全.消息无限制等特 ...

  4. Java 在Word中创建邮件合并模板并合并文本和图片

    Word里面的邮件合并功能是一种可以快速批量操作同类型数据的方式,常见的如数据填充.打印等.其中必不可少的步骤包括用于填充的模板文档.填充的数据源以及实现邮件合并的功能.下面,通过Java程序展示如何 ...

  5. 【原】移动web页面给用户发送邮件的方法 (邮件含文本、图片、链接)

    微信商户通有这么一个需求,用户打开H5页面后,引导用户到电脑下载设计资源包,由于各种内部原因,被告知无后台资源支持,自己折腾了一段时间找了下面2个办法,简单做下笔记. 使用mailto功能,让用户自己 ...

  6. C# 创建邮件合并模板并合并文本、图片

    对于Word中的邮件合并功能,用户可以将邮件合并后的结果文档保存并打印,也可以通过邮件的形式发送,在很多场合需要使用到此功能.那对于编程人员,我们也可以在C#语言环境中通过代码的形式来实现.根据需要先 ...

  7. contents() 查找匹配元素内部所有的子节点(包括文本节点)。如果元素是一个iframe,则查找文档内容

    contents() V1.2概述 查找匹配元素内部所有的子节点(包括文本节点).如果元素是一个iframe,则查找文档内容   示例 描述:大理石平台检定规程 查找所有文本节点并加粗 HTML 代码 ...

  8. C# 如何添加Word文本和图片超链接

    超链接简单来讲就是内容链接,通过设置超链接可以实现对象与网页.站点之间的连接.链接目标可以是网页.图片.邮件地址.文件夹或者是应用程序.设置链接的对象可以是文本或者图片. 在以下内容中,我将介绍如何用 ...

  9. 用Python教你微信防撤回(文本、图片、语音、视频、名片等...)

    大家在使用微信过程中,有时候消息还没看到,就被撤回了.毕竟好奇心大家都有,明知到消息被撤回了,就更想去看一下是什么内容心里想着万一是女神给我表白了呢.. 今天就用Python来做个微信防撤回的小功能. ...

随机推荐

  1. 基于Qt的第三方库和控件

    ====================== 基于Qt的第三方库和控件 ======================     libQxt --------   http://dev.libqxt.o ...

  2. 解决iScroll中事件点击一次却触发两次的问题

    var t1=null;//全局 function myClick() { if (t1 == null){ t1 = new Date().getTime(); }else{ var t2 = ne ...

  3. SQL_server 的基本操作

    1.---------------数据库基本操作 主键 : 1.不重复 2.不为NULL外键 1.取消重复行(消除完全一样的行,保留一行)select distinct cloumname1,clou ...

  4. WisDom.Net 框架设计(七) 验证框架

    WisDom.Net-验证框架 1.分类 这里我们将数据验证分为以下几种 数据类型校验      主要用于确保数据类型输入的正确  比如年龄一项输入 A岁 ,显然不合法 域检查             ...

  5. google map 定位

    在map初始化的过程中,得到当前经纬度,完成初始化地图,通过HTML5中的Geolocation实现,具体参考:http://www.jb51.net/html5/71556.html 1.获取当前地 ...

  6. Ubuntu下Hadoop快速安装手册

    http://www.linuxidc.com/Linux/2012-02/53106.htm 一.环境 Ubuntu 10.10+jdk1.6 二.下载&安装程序 1.1 Apache Ha ...

  7. iOS崩溃报告获取一

    在AppDelegate.m文件中实现函数 void UncaughtExceptionHandler(NSException *exception) { /** * 获取异常崩溃信息 */ NSAr ...

  8. QT QString转char*,char*转QString;简单明了,看代码。

    //原始QStringQString qs = QString::fromLocal8Bit("我的");std::string strQs = qs.toStdString(); ...

  9. 南理第八届校赛同步赛-F sequence//贪心算法&二分查找优化

    题目大意:求一个序列中不严格单调递增的子序列的最小数目(子序列之间没有交叉). 这题证明贪心法可行的时候,可以发现和求最长递减子序列的长度是同一个方法,只是思考的角度不同,具体证明并不是很清楚,这里就 ...

  10. C++判断Office版本——转载

    自:http://blog.csdn.net/lpc_china/article/details/18359145 主要原理:查询windows注册表microsoft office软件项的值来判断版 ...