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

附件,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. MYSQL 5.7 MTS 复制

    http://www.linuxidc.com/Linux/2013-04/82712p2.htm http://keithlan.github.io/2016/06/28/MTS/ http://d ...

  2. careercup-栈与队列 3.1

    3.1 描述如何只用一个数组来实现三个栈. 解答 我们可以很容易地用一个数组来实现一个栈,压栈就往数组里插入值,栈顶指针加1: 出栈就直接将栈顶指针减1:取栈顶值就把栈顶指针指向的单元的值返回: 判断 ...

  3. Cocos2d-X中字符串的处理

    CCString 用惯了NSString,你会严重高估自己处理字符串的能力.使用Cocos2d-X后只能用char*或者string来代替.诸如字符串的拼接,替换,查找都比NSString麻烦不少. ...

  4. mybatis缓存清除方法

    String cacheName = IWenshiduDao.class.getName(); Ehcache cache = CacheManager.create().getEhcache(ca ...

  5. PGsql解决时差24H

    SELECT sa_ed_time, sa_st_time, case when sa_ed_time > sa_st_time then extract(EPOCH FROM (sa_ed_t ...

  6. UDP,TCP理解。

    UDP: 面向无连接, 每个数据大小限制在64K内 因为面向无连接,所以就是不可靠协议. 将数据和源和谜底封装到数据包当中,不需要建立连接.速度快(就像送快递一样,管你在不可以先到你门口) 用处:聊天 ...

  7. 导出excel的简单方法

    excel的操作,最常用的就是导出和导入,废话不多说上代码. 本例使用NPOI实现的,不喜勿喷哈.... /// <summary> /// 导出Excel /// </summar ...

  8. css - div垂直方向滚动

    只要设置 OVERFLOW-Y:auto;OVERFLOW-X:hidden即可.

  9. DataList分页-增加自动编号列

    <asp:DataList ID="dl_XUDAXIA" runat="server"> <HeaderTemplate> <t ...

  10. Linux如何查找大文件或目录总结-1127

    原帖地址:http://www.cnblogs.com/kerrycode/p/4391859.html  谢谢潇湘隐者,谢谢老大 在Linux系统中,如何去搜索一些比较大的文件呢?下面我整理了一下在 ...