发送邮件前提:开启邮箱授权码
一、开启授权码(以163邮箱为例)
1.登录163邮箱,点击设置--POP3/SMTP/IMAP,出现设置界面
 
2. 开启SMTP服务且可以查询SMTP的host地址
 
3.设置界面选择--客户端授权密码,出现如图界面,点击开启授权码,需要短信验证,自己设置授权码,启之后也可以重置授权码,当然也需要短信验
4.客户端授权密码设置完之后,保持登录
 
二、开启qq授权码
邮箱中设置-账户-POP3/IMAP...这一项,开启POP3/SMTP服务
点击开启的时候,会让发送短信验证,照着操作就行,收取选项选择全部

设置完成之后一定要记得保存

 
三、发送邮件
python发送邮件,使用smtplib、MIMEText模块
1.163邮箱发送邮件
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笔记之发送邮件的更多相关文章

  1. python笔记- 发送邮件

    依赖: Python代码实现发送邮件,使用的模块是smtplib.MIMEText,实现代码之前需要导入包: import smtplib from email.mime.text import MI ...

  2. Python笔记之不可不练

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

  3. python笔记 - day3

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

  4. boost.python笔记

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

  5. 20.Python笔记之SqlAlchemy使用

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

  6. Python笔记——类定义

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

  7. 13.python笔记之pyyaml模块

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

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

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

  9. python笔记 - day8

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

随机推荐

  1. Vue插槽的另外一些特性

    之前有个项目,想判断一下,某一个模板内的插槽是否被使用. 不知道是不是问题过于简单,网上没有这方面的说明.我就抽时间验证一下vue插槽相关的一些功能. 然后写下这篇随笔,希望对后来人能有一些帮助. 简 ...

  2. HDU1026 Ignatius and the Princess I 【BFS】+【路径记录】

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  3. 从一个简单的例子谈谈package与import机制

    转,原文:http://annie09.iteye.com/blog/469997 http://blog.csdn.net/gdsy/article/details/398072 这两篇我也不知道到 ...

  4. 1. MaxCounters 计数器 Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum.

    package com.code; import java.util.Arrays; public class Test04_4 { public static int[] solution(int ...

  5. Codeforces Round #316 (Div. 2) C. Replacement(线段树)

    C. Replacement time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  6. Linux下的画图软件

    Pinta是一款和windows下的画图相类似打一款画图软件,并且它还包含了一些基本的图像编辑工具. 比如:标尺.图层.操作历史记录.图像调整.渲染效果等等,可以满足对图像处理要求不太高的用户的基本需 ...

  7. zoj 1610 Count the Colors 【区间覆盖 求染色段】

    Count the Colors Time Limit: 2 Seconds      Memory Limit: 65536 KB Painting some colored segments on ...

  8. 三种常见的编码:ASCII码、UTF-8编码、Unicode编码等字符占领的字节数

    ASCII码: 一个英文字母(不分大写和小写)占一个字节的空间.一个中文汉字占两个字节的空间. 一个二进制数字序列,在计算机中作为一个数字单元,一般为8位二进制数,换算为十进制. 最小值0,最大值25 ...

  9. 技术架构model

  10. Easyui 页面訪问慢解决方式,GZIP站点压缩加速优化

    1. 静态资源压缩GZIP是站点压缩加速的一种技术,对于开启后能够加快我们站点的打开速度.原理是经过server压缩,client浏览器高速解压的原理,能够大大降低了站点的流量. 详细代码能够參加je ...