依赖:

Python代码实现发送邮件,使用的模块是smtplib、MIMEText,实现代码之前需要导入包:

import smtplib
from email.mime.text import MIMEText

使用163邮件发送邮件,具体代码实现如下:

import smtplib
from email.mime.text import MIMEText
'''
发送邮件函数,默认使用163smtp
:param mail_host: 邮箱服务器,16邮箱host: smtp.163.com
:param port: 端口号,163邮箱的默认端口是 25
:param username: 邮箱账号 xx@163.com
:param passwd: 邮箱密码(不是邮箱的登录密码,是邮箱的授权码)
:param recv: 邮箱接收人地址,多个账号以逗号隔开
:param title: 邮件标题
:param content: 邮件内容
:return:
''' def send_mail(username, passwd, recv, title, content, mail_host='smtp.163.com', port=25):
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 = 'xxxx@163.com' # 发送者账号
email_pwd = 'xxxxx' # 发送者密码,授权码
maillist = 'xxxx@qq.com'
title = '测试邮件标题'
content = '这里是邮件内容'
send_mail(email_user, email_pwd, maillist, title, content)

163邮箱的授权码获取方法如下:

1. 登录163邮箱,点击设置-POP3/SMTP/IMAP,如下:

2. 开启SMTP服务且可以查询SMTP的host地址:

3. 点击客户端授权密码,可以重置授权码:

使用QQ邮件发送邮件,具体代码实现如下:

import smtplib
from email.mime.text import MIMEText
'''
发送邮件函数,默认使用163smtp
:param mail_host: 邮箱服务器,qq邮箱host: smtp.qq.com
:param port: 端口号,qq邮箱的默认端口是: 456
:param username: 邮箱账号 xx@163.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=456):
msg = MIMEText(content) # 邮件内容
msg['Subject'] = title # 邮件主题
msg['From'] = username # 发送者账号
msg['To'] = recv # 接收者账号列表
smtp = smtplib.SMTP_SSL(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 = 'xxx@qq.com' # 发送者账号
email_pwd = 'WOSHINIGE123' # 发送者密码,授权码
maillist = 'xxxx@qq.com'
title = '测试邮件标题'
content = '这里是邮件内容'
send_mail(email_user, email_pwd, maillist, title, content)

多个 收件人且带附件:

# import smtplib
# from email.mime.text import MIMEText
# '''
# 发送邮件函数,默认使用163smtp
# :param mail_host: 邮箱服务器,qq邮箱host: smtp.163.com
# :param port: 端口号,qq邮箱的默认端口是: 25
# :param username: 邮箱账号 xx@163.com
# :param passwd: 邮箱密码(不是邮箱的登录密码,是邮箱的授权码)
# :param recv: 邮箱接收人地址,多个账号以逗号隔开
# :param title: 邮件标题
# :param content: 邮件内容
# :return:
# '''
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart #邮箱服务器地址
email_host = 'smtp.163.com'
#发送者
email_user = 'xxxx@163.com'
#授权码
email_pwd = 'WOSHINIGE123'
#多个收件人,使用分号分隔
maillist ='xxxx@qq.com;aaaa@qq.com'
#对多个收件人进行分割,以;为标准,返回结果是list['xxxx@qq.com','aaaa@qq.com']
email_info = maillist.split(';')
#构造一个发附件的邮件内容对象
new_msg = MIMEMultipart()
#邮件正文内容
new_msg.attach(MIMEText('test email.....'))
#邮件主题
new_msg['Subject'] = 'test email'
#邮件发送者
new_msg['From'] = email_user
#邮件接收者,多个收件人的账号使用,连接,传入类型是str
new_msg['To'] = ','.join(email_info)
#打开a.txt读取文本内容
att = MIMEText(open('a.txt').read())
att["Content-Type"] = 'application/octet-stream'
# 发送附件就得这么写,固定格式,filename指定附件的名字
att["Content-Disposition"] = 'attachment; filename="haha.txt"'
new_msg.attach(att)
# 连接邮箱,传入邮箱地址,和端口号,smtp的端口号是25
smtp = smtplib.SMTP(email_host, port=25)
# 发送者的邮箱账号,密码,先登录
smtp.login(email_user, email_pwd)
smtp.sendmail(email_user, email_info, new_msg.as_string())
smtp.quit()
print('email send success.')

多个收件人时账号之间使用分号;分割,进行smtp.sendmail(传入的多个收件人类型是list);new_msg['TO'] = recv,接收的类型是str

使用类实现邮件的发送,即可发送多人也可发送附件:

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())
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="%s"'%self.file
msg.attach(att)
msg.attach(MIMEText(self.content))#邮件正文的内容
msg['Subject'] = self.title # 邮件主题
msg['From'] = self.username # 发送者账号
#将多个账号'aaa.@qq.com;bbbb@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='zzzzz@163.com', passwd='xxxxxxx',file='a.txt', recv='xxxxxx@qq.com;xxxxxx@qq.com',
title='多个收件人', content='哈哈哈啊哈哈哈哈', email_host='smtp.163.com', port=25
)
m.send_ma

欢迎多多评论~~~~

python笔记- 发送邮件的更多相关文章

  1. Python笔记之不可不练

    如果您已经有了一定的Python编程基础,那么本文就是为您的编程能力锦上添花,如果您刚刚开始对Python有一点点兴趣,不怕,Python的重点基础知识已经总结在博文<Python笔记之不可不知 ...

  2. python笔记 - day3

    python笔记 - day3 参考:http://www.cnblogs.com/wupeiqi/articles/5453708.html set特性: 1.无序 2.不重复 3.可嵌套 函数: ...

  3. boost.python笔记

    boost.python笔记 标签: boost.python,python, C++ 简介 Boost.python是什么? 它是boost库的一部分,随boost一起安装,用来实现C++和Pyth ...

  4. 20.Python笔记之SqlAlchemy使用

    Date:2016-03-27 Title:20.Python笔记之SqlAlchemy使用 Tags:python Category:Python 作者:刘耀 博客:www.liuyao.me 一. ...

  5. Python笔记——类定义

    Python笔记——类定义 一.类定义: class <类名>: <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性 如果直接使用类名修改其属 ...

  6. 13.python笔记之pyyaml模块

    Date:2016-03-25 Title:13.Python笔记之Pyymal模块使用 Tags:Python Category:Python 博客地址:www.liuyao.me 作者:刘耀 YA ...

  7. 8.python笔记之面向对象基础

    title: 8.Python笔记之面向对象基础 date: 2016-02-21 15:10:35 tags: Python categories: Python --- 面向对象思维导图 (来自1 ...

  8. python笔记 - day8

    python笔记 - day8 参考: http://www.cnblogs.com/wupeiqi/p/4766801.html http://www.cnblogs.com/wupeiqi/art ...

  9. python笔记 - day7-1 之面向对象编程

    python笔记 - day7-1 之面向对象编程 什么时候用面向对象: 多个函数的参数相同: 当某一些函数具有相同参数时,可以使用面向对象的方式,将参数值一次性的封装到对象,以后去对象中取值即可: ...

随机推荐

  1. Textarea自动适用高度且无滚动条解决方案

    今日的系统需要动态显示一项数据库里面的内容,该内容包含换行等格式字符,要求如实的反应在页面上. 最初解决办法是使用textarea控件,代码如下: <textarea style="b ...

  2. set_exception_handler 自定义异常处理

    该函数用于创建运行时期间的用户自己的异常处理方法. set_exception_handler(error_function) 参数 必需.规定未捕获的异常发生时调用的函数. 该函数必须在调用 set ...

  3. Js、JQuery脚本兼容

    1.获取属性值 IE:$("#xxx").prop("title"); chrome:$("#xxx").attr("title& ...

  4. Java创建和读取Json

    在Java中构造和解析JSON我用的是org.json,附件为相应的org.json.jar. 下面是两个函数,一个是创建JSON,一个是从文本构造JSON并解析之. 创建json: //constr ...

  5. Android 开发之使用Eclipse Debug调试详解(转)

    转自 http://blog.csdn.net/xys289187120/article/details/6636331 1.在程序中添加一个断点 如果所示:在Eclipse中添加了一个程序断点 在E ...

  6. DDL语句--改动表

    改动表是指改动数据库中已经存在的表的定义.改动表比又一次定义表简单.不须要又一次载入数据.也不会影响正在进行的服务. MySQL中通过ALTER TABLE语句来改动表.改动表包含改动表名.改动字段数 ...

  7. test_login

    import unittest,requestsimport ddtfrom BeautifulReport import BeautifulReport as bffrom urllib impor ...

  8. 使用Parallel实现简单的并行操作

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  9. 1 bootstrap table null默认显示为 - 要查源码 2 记一个很无语的bug

    本来返回的json 3个true 7个false的 结果显示10个true 因为本来是好的 结果判断的问题 给全部赋值true了

  10. zookeeper(二):linux centos下安装zookeeper(单机和集群)

    下载 http://zookeeper.apache.org/releases.html 解压 tar –zxvf zookeeper-3.4.6.tar.gz 解压文件到"/usr/loc ...