python邮件发送实例_python 七种邮件内容发送方法实例
一、文件形式的邮件
代码如下:#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’
msg = MIMEText(‘你好’,’text’,’utf-8′)#中文需参数‘utf-8’,单字节字符不需要
msg[‘Subject’] = Header(subject, ‘utf-8’)
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
二、HTML形式的邮件
代码如下:
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’
msg = MIMEText(‘/pre
h1你好/h1
pre’,’html’,’utf-8′)
msg[‘Subject’] = subject
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
三、带图片的HTML邮件
代码如下:
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’
msgRoot = MIMEMultipart(‘related’)
msgRoot[‘Subject’] = ‘test message’
msgText = MIMEText(‘bSome iHTML/i text/b and an image.
img alt=”” src=”cid:image1″ /
good!’,’html’,’utf-8′)
msgRoot.attach(msgText)
fp = open(‘h:\\python\\1.jpg’, ‘rb’)
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header(‘Content-ID’, ”)
msgRoot.attach(msgImage)
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
四、带附件的邮件
代码如下:#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’
msgRoot = MIMEMultipart(‘related’)
msgRoot[‘Subject’] = ‘test message’
#构造附件
att = MIMEText(open(‘h:\\python\\1.jpg’, ‘rb’).read(), ‘base64’, ‘utf-8’)
att[“Content-Type”] = ‘application/octet-stream’
att[“Content-Disposition”] = ‘attachment; filename=”1.jpg”‘
msgRoot.attach(att)
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
五、群邮件
代码如下:#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
sender = ‘***’
receiver = [‘***’,’****’,……]
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’
msg = MIMEText(‘你好’,’text’,’utf-8′)
msg[‘Subject’] = subject
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
六、各种元素都包含的邮件
代码如下:#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’
# Create message container – the correct MIME type is multipart/alternative.
msg = MIMEMultipart(‘alternative’)
msg[‘Subject’] = “Link”
# Create the body of the message (a plain-text and an HTML version).
text = “Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org”
html = “””\
Hi!
How are you?
Here is the a href=”http://www.python.org”link/a you wanted.
“””
# Record the MIME types of both parts – text/plain and text/html.
part1 = MIMEText(text, ‘plain’)
part2 = MIMEText(html, ‘html’)
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
#构造附件
att = MIMEText(open(‘h:\\python\\1.jpg’, ‘rb’).read(), ‘base64’, ‘utf-8’)
att[“Content-Type”] = ‘application/octet-stream’
att[“Content-Disposition”] = ‘attachment; filename=”1.jpg”‘
msg.attach(att)
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
七、基于SSL的邮件
代码如下:#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = ‘***’
receiver = ‘***’
subject = ‘python email test’
smtpserver = ‘smtp.163.com’
username = ‘***’
password = ‘***’
msg = MIMEText(‘你好’,’text’,’utf-8′)#中文需参数‘utf-8’,单字节字符不需要
msg[‘Subject’] = Header(subject, ‘utf-8’)
smtp = smtplib.SMTP()
smtp.connect(‘smtp.163.com’)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.set_debuglevel(1)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
python邮件发送实例_python 七种邮件内容发送方法实例相关推荐
- python队列怎么用_Python队列的定义与使用方法实例详解
这篇文章主要介绍了Python队列的定义与使用方法,结合具体实例形式分析了Python定义及使用队列的具体操作技巧与注意事项,需要的朋友可以参考下 本文实例讲述了Python队列的定义与使用方法.分享 ...
- python实时监控文件大小_python实现实时监控文件的方法
在业务稳定性要求比较高的情况下,运维为能及时发现问题,有时需要对应用程序的日志进行实时分析,当符合某个条件时就立刻报警,而不是被动等待出问题后去解决,比如要监控nginx的$request_time和 ...
- 笔记本网络计算机和设备不可见,xp电脑不显示无线网络的七种原因和解决方法...
xp纯净版系统电脑打开后发现桌面右下角不显示无线网络,如果要设置无线网络都不知道从哪里下手,这到底是怎么回事?造成xp系统不显示无线网络的原因有很多种,下面和大家讲解一下xp电脑不显示无线网络的七种原 ...
- C#Winform的DataGridView控件使用详解1—七种DataGridViewColumn类型使用方法
C#Winform的DataGridView控件使用详解1-七种DataGridViewColumn类型使用方法 DataGirdView控件Column类型 DataGridViewButtonCo ...
- python生日祝福短信_python自动发送生日祝福邮件
最近刚开学,班里的事也挺多,大多都涉及到excel的使用.本来我excel用得不是很熟,很多高级的操作都不会.比如给两列的交赋值等.在网上搜了下,发现这些操作的实现都涉及到excel内部的一些命令,而 ...
- python 子串是否在字符串中_python七种方法判断字符串是否包含子串
1. 使用 in 和 not in in 和 not in 在 Python 中是很常用的关键字,我们将它们归类为 成员运算符. 使用这两个成员运算符,可以很让我们很直观清晰的判断一个对象是否在另一个 ...
- python web游戏实例_python实现的简单文本类游戏实例
Python应用与实践 Python应用与实践 目录 1. Python是什么? 1.1. Python语言 1.2. Python哲学 2. Python在工 ...
- python timer怎么用_python定时器(Timer)用法简单实例
python定时器(Timer)用法简单实例 本文实例讲述了python定时器(Timer)用法.分享给大家供大家参考.具体如下: # encoding: UTF-8 import threading ...
- Spring容器中获取Bean实例的七种方式(附实战源码)
目录 写作说明 一:写作原因 二:源码出处 实现方式 一:使用BeanFactory直接获取(不推荐) 二:在初始化时保存ApplicationContext对象 三:继承自抽象类Applicatio ...
最新文章
- 【吾日三省吾身】2015.5.24-涅槃行动第六天
- xcode6 AsynchronousTesting 异步任务测试
- robotframework手机号随机产生脚本
- SVM学习(三):线性分类器的求解
- 反射获取私有构造方法并运行
- python如何更改entry属性_如何在Python3中更改Gtk3 Entry文本颜色?
- HTML5商城开发四 多图或多商品的水平滚动展示
- 任何性能指标越界或造成 APP 崩溃,优酷通用性能测试一招搞定
- jpa 托管,Apache Aries托管JPA支持哪些JPA提供程序?
- 接入网+承载网+核心网
- MySQL入门系列:查询简介(二) 过滤数据
- 比較好的JAVA網站
- 阿里支付宝api对银行卡进行效验
- 在可见光波长下用银树突超表面进行微分运算
- Cassandra_教程二_利用 CQL 操作 Cassandra
- 宇宙的精华与大数据的失败
- PostgreSQL创建存储过程
- 如何压缩jpg图片的大小?可以一键压缩图片的软件有哪些?
- 自定义IP地址安装固定驱动柯美c266打印机,并自动销毁安装程序
- USACO-beads