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

附件,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. 【转】Enable ARC in a Cocos2D Project: The Step-by-Step-How-To-Guide Woof-Woof!

    On April 5, 2012, in idevblogaday, by Steffen Itterheim http://www.learn-cocos2d.com/2012/04/enablin ...

  2. JAVA格式化时间日期

    JAVA格式化时间日期 import java.util.Date; import java.text.DateFormat; /** * 格式化时间类 * DateFormat.FULL = 0 * ...

  3. 用java读取properties文件--转

    今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享.     下面直接贴出代码:java类 public class Mytest pub ...

  4. [Eclipse]The type XXX cannot be resolved. It is indirectly referenced from required .class files

    在Eclipse中遇到The type XXX cannot be resolved. It is indirectly referenced from required .class files错误 ...

  5. 解决升级windows8.1 Oracle服务被刷新

    1.调用CMD管理员模式,记得,否则你想要执行的东西都调用不了,win8下“窗口键+X”-“命令提示符(管理员) 2.创建oracle10g.11g的监听服务:(%ORACLE_HOME%为oracl ...

  6. Java基础知识强化之集合框架笔记59:Map集合之TreeMap(TreeMap<String,String>)的案例

    1. TreeMap类的概述: 键是红黑树结构,可以保证键的排序和唯一性. 2. TreeMap案例: TreeMap<String, String> 代码示例: package cn.i ...

  7. CI框架篇之基础篇(2)

    CodeIgniter 的基础了解了后,现在就来对这个框架进行预热 CodeIgniter 配置 理论是不用配置,直接拷贝到服务器目录下即可运行 CodeIgniter 安装分为四个步骤: 1. 解压 ...

  8. Java-Android 之出滚动条和卷轴页面

    <?xml version="1.0" encoding="utf-8"?> <HorizontalScrollView xmlns:andr ...

  9. oracle学习笔记4:PL/SQL

    PL/SQL是没命名的存储过程,函数,触发器,PL/SQL块的语法格式如下: [declare] --声明部分,可选 begin --执行部分,必须 [exception] --异常处理部分,可选 e ...

  10. MyEclipse中配置自己的JRE和tomcat

    MyEclipse中配置自己的JRE:windows>Preference>java>Installed JREs>Add>Stantard VM>next> ...