python笔记之发送邮件




设置完成之后一定要记得保存
import smtplib
from email.mime.text import MIMEText def send_mail(username, passwd, recv, title, content, mail_host='smtp.163.com', port=25):
'''
发送邮件函数,默认使用163smtp
:param username: 邮箱账号 xx@163.com
:param passwd: 邮箱授权码,不是邮箱密码
:param recv: 邮箱接收人地址,多个账号以逗号隔开
:param title: 邮件标题
:param content: 邮件内容
:param mail_host: 邮箱服务器
:param port: 端口号
:return:
'''
msg = MIMEText(content) # 邮件内容
msg['Subject'] = title # 邮件主题
msg['From'] = username # 发送者账号
msg['To'] = recv # 接收者账号列表
smtp = smtplib.SMTP(mail_host, port=port) # 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25
smtp.login(username, passwd) # 发送者的邮箱账号,密码
smtp.sendmail(username, recv, msg.as_string())
# 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
smtp.quit() # 发送完毕后退出smtp
print('email send success.') if __name__ =="__main__":
email_user = 'xxxxx@163.com' # 发送者账号
email_pwd = 'xxxxx' # 发送者邮箱授权码
maillist = 'xxxx@qq.com'#收件邮箱
title = '测试邮件标题'
content = '这里是邮件内容'
send_mail(email_user, email_pwd, maillist, title, content)
2.qq邮箱发送邮件
import smtplib
from email.mime.text import MIMEText
'''
:param mail_host: 邮箱服务器,qq邮箱host: smtp.qq.com
:param port: 端口号,qq邮箱的默认端口是: 465
:param username: 邮箱账号 xx@qq.com
:param passwd: 邮箱密码(不是邮箱的登录密码,是邮箱的授权码)
:param recv: 邮箱接收人地址,多个账号以逗号隔开
:param title: 邮件标题
:param content: 邮件内容
:return:
'''
#qq邮箱发送邮件
def send_mail(username, passwd, recv, title, content, mail_host='smtp.qq.com', port=465):
msg = MIMEText(content) # 邮件内容
msg['Subject'] = title # 邮件主题
msg['From'] = username # 发送者账号
msg['To'] = recv # 接收者账号列表
smtp = smtplib.SMTP_SSL(mail_host, port=port) # 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是465
smtp.login(username, passwd) # 登录发送者的邮箱账号,密码
# 参数分别是 发送者,接收者,第三个是把上面的发送邮件的 内容变成字符串
smtp.sendmail(username, recv, msg.as_string())
smtp.quit() # 发送完毕后退出smtp
print('email send success.') if __name__ == '__main__':
email_user = 'xxx@qq.com' # 发送者账号
email_pwd = 'xxxx' # 发送者密码,授权码
maillist = 'xxx@qq.com'
title = '测试邮件标题'
content = '这里是邮件内容'
send_mail(email_user, email_pwd, maillist, title, content)
3.发送给多人带多个附件的邮件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart class SendMail(object):
def __init__(self, username, passwd, recv, title, content, file=None, email_host='smtp.163.com', port=25):
self.username = username
self.passwd = passwd
self.recv = recv
self.title = title
self.content = content
self.file = file
self.email_host = email_host
self.port = port def send_mail(self):
msg = MIMEMultipart()
#发送内容的对象
if self.file:#处理附件的
att = MIMEText(open(self.file, encoding='utf-8').read())#如果图片,指定一下打开模式'rb'
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="%s"'%self.file att2 = MIMEText(open(self.file, encoding='utf-8').read())
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="%s"' % self.file
msg.attach(att)
msg.attach(att2)
msg.attach(MIMEText(self.content))#邮件正文的内容
msg['Subject'] = self.title # 邮件主题
msg['From'] = self.username # 发送者账号
#将多个账号'xxx@qq.com;xxx@163.com' 以分号分割,分割为list
self.recv = self.recv.split(';')
if isinstance(self.recv, list):
msg['To'] = ','.join(self.recv)
else:
msg['To'] = self.recv # 接收者账号列表
if self.username.endswith('qq.com'): #如果发送者是qq邮箱
self.smtp = smtplib.SMTP_SSL(self.email_host, port=self.port)
else:
self.smtp = smtplib.SMTP(self.email_host, port=self.port)
#发送邮件服务器的对象
self.smtp.login(self.username, self.passwd)
try:
self.smtp.sendmail(self.username, self.recv, msg.as_string())
except Exception as e:
print('出错了。。', e)
else:
print('发送成功!')
self.smtp.quit() if __name__ == '__main__':
m = SendMail(
username='xxx@163.com', passwd='xxx',file='hhf.txt', recv='xx@qq.com;xx@qq.com',
title='多个收件人', content='eee', email_host='smtp.163.com', port=25
)
m.send_mail()
python笔记之发送邮件的更多相关文章
- python笔记- 发送邮件
依赖: Python代码实现发送邮件,使用的模块是smtplib.MIMEText,实现代码之前需要导入包: import smtplib from email.mime.text import MI ...
- Python笔记之不可不练
如果您已经有了一定的Python编程基础,那么本文就是为您的编程能力锦上添花,如果您刚刚开始对Python有一点点兴趣,不怕,Python的重点基础知识已经总结在博文<Python笔记之不可不知 ...
- python笔记 - day3
python笔记 - day3 参考:http://www.cnblogs.com/wupeiqi/articles/5453708.html set特性: 1.无序 2.不重复 3.可嵌套 函数: ...
- boost.python笔记
boost.python笔记 标签: boost.python,python, C++ 简介 Boost.python是什么? 它是boost库的一部分,随boost一起安装,用来实现C++和Pyth ...
- 20.Python笔记之SqlAlchemy使用
Date:2016-03-27 Title:20.Python笔记之SqlAlchemy使用 Tags:python Category:Python 作者:刘耀 博客:www.liuyao.me 一. ...
- Python笔记——类定义
Python笔记——类定义 一.类定义: class <类名>: <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性 如果直接使用类名修改其属 ...
- 13.python笔记之pyyaml模块
Date:2016-03-25 Title:13.Python笔记之Pyymal模块使用 Tags:Python Category:Python 博客地址:www.liuyao.me 作者:刘耀 YA ...
- 8.python笔记之面向对象基础
title: 8.Python笔记之面向对象基础 date: 2016-02-21 15:10:35 tags: Python categories: Python --- 面向对象思维导图 (来自1 ...
- python笔记 - day8
python笔记 - day8 参考: http://www.cnblogs.com/wupeiqi/p/4766801.html http://www.cnblogs.com/wupeiqi/art ...
随机推荐
- vijos- P1383盗窃-黑珍珠 (python + 代码优化)
P1383盗窃-黑珍珠 Accepted 标签:怪盗基德 VS OIBH[显示标签] 背景 怪盗基德 VS OIBH 第二话 描写叙述 今次怪盗基德再次对阵OIBH,目标是Black Star!基德已 ...
- python 除法总返回浮点
python 除法总返回浮点,要返回整型需要用//: print(type(4/2),type(4//2)) #<class 'float'> <class 'int'>
- HTML5:表格
表格的作用是显示二维数据.在HTML5中不再同意用表格控制页面内容的布局.而是採用新增的CSS表格特性(这里不涉及CSS,将在后面介绍). 以下主要介绍用于制作表格的HTML元素. 构建表格 表格的基 ...
- Win7 无法安装Office source engine 足够的权限安装系统服务怎么办
运行CMD,输入命令:sc delete ose 重试即可.
- WingIDE 5.0注冊机
在wingIDE下开发python很方便,但IDE不是免费的,网上有破解的方法.请支持正版. 把下列文件CalcActivationCode.py载入到wingIDE中.LicenseID能够随便给一 ...
- 读写Word的组件DocX介绍与入门
本文为转载内容: 文章原地址:http://www.cnblogs.com/asxinyu/archive/2013/02/22/2921861.html 开源Word读写组件DocX介绍与入门 阅读 ...
- UVa563_Crimewave(网络流/最大流)(小白书图论专题)
解题报告 思路: 要求抢劫银行的伙伴(想了N多名词来形容,强盗,贼匪,小偷,sad.都认为不合适)不在同一个路口相碰面,能够把点拆成两个点,一个入点.一个出点. 再设计源点s连向银行位置.再矩阵外围套 ...
- PHPthinking官方论坛招募版主
时间飞逝.就在昨天,我们PHPthinking的官方论坛刚刚上线了我们自己的论坛! 欢迎大家注冊账号,活跃在论坛的大家庭中,我们会及时关注论坛公布的全部内容.在开发学习的过程中,遇到的不论什么问题,有 ...
- Intellij IDEA报错:Could not save application settings: java.io.IOException: java.lang.AssertionError: Unexpected content storage modificat
Question: i have a message saying "Could not save application settings: java.io.IOException: ja ...
- MongoDB使用初步
我很快就要离开现在这个使用nodejs + mongodb + redis的项目,转而去搞 塞特ID 之类的别的项目了.可惜这些技术对我来说浅尝辄止,半生不熟,胎死腹中.业余时间自学当然也可以,但哪有 ...