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

附件,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. qemu 的方式安装debian 模拟powerpc

    http://bbs.pediy.com/showthread.php?p=1424746http://www.ibm.com/developerworks/cn/linux/l-qemu/ 线总结下 ...

  2. 启动hadoop,报错Error JAVA_HOME is not set and could not be found

    报如错误:JAVA_HOME is not set and could not be found,可能是因为JAVA_HOME环境没配置正确,还有一种情况是即使各结点都正确地配置了JAVA_HOME, ...

  3. [学习笔记]设计模式之Adapter

    写在前面 为方便读者,本文已添加至索引: 设计模式 学习笔记索引 Adapter(适配器)模式主要解决接口不匹配的问题.为此,让我们要回到最初Builder模式创建平行世界时,白雪公主和小霍比特人的谜 ...

  4. CentOS7系统下搭建Jenkins环境

    1. 安装JDK yum -y install java 2.安装Ant 添加JPackage源 yum -y install wget wget http://www.jpackage.org/jp ...

  5. HTML5 Canvas 2D绘图

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/4851774. ...

  6. 借用网上大神的一些知识,html5 video 视频播放都兼容(Android,iOS,电脑)

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...

  7. HDU-1015(暴力)

    Safecracker Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is loc ...

  8. gulp 初体验

    1,全局安装 gulp npm install --global gulp 但是一直无法成功,后来才知被墙了,于是使用了如下命令,安装cnpm npm install -g cnpm --regist ...

  9. android--HttpURLConnection(转载)

    android之HttpURLConnection 1.HttpURLConnection连接URL1)创建一个URL对象 URL url = new URL(http://www.baidu.com ...

  10. Android - This Handler class should be static or leaks might occur.

    今天学习了使用 HTTP协议,从Android客户端往Tomcat服务器端以GET发送请求,途中无意中发现自己写的Handler类被Android提示:This Handler class shoul ...