python使用smtplib和email库发送邮件
国内很多服务器提供商都默认禁止了smtp默认的25端口服务,而启用465端口发送邮件
在smtplib库中直接调用SMTP_SSL就是默认使用465端口
示例代码如下:
def send_eamil(receiver):
global msg
sender = 'xxx@163.com'
psd = 'xxx' #是授权密码不是邮箱登录密码
mail_host='smtp.163.com'
subject='xxx'
to_addrs=receiver.split(',')
#创建带附件的实例
msg=MIMEMultipart()
msg['Subject']=Header(subject,'utf-8')
msg['from']=Header(sender,'utf-8')
msg['To']=",".join(to_addrs) #多个收件人
#创建正文,把文本添加到msg类中
msg.attach(MIMEText('Push test','plain','utf-8'))
print('set ok')
# #构造附件
# file_path='文件路径'
# att1=MIMEText(open(file_path,'rb').read(),
# 'base64','utf-8')
# #att1["Content-Type"]='application/octet-stream;name=%s'%Header(filename,'utf-8').encode('utf-8') #与下边功能一致
# #att1["Content-Disposition"]='attachment;file_name=%s'%Header('%s'%filename,'utf-8').encode('utf-8') #与下边功能一致
# att1.add_header('Content-Disposition', 'attachment', filename=名字随便起)#没有这三行会出现文件结尾变成bin现象
# att1.add_header('Content-ID', '<0>')
# att1.add_header('X-Attachment-Id', '0')
# msg.attach(att1)#将附件添加到类文件
try:
smtp=SMTP_SSL(mail_host)
smtp.set_debuglevel(1) #显示发送过程
# smtp.connect(mail_host)
print('connect ok')
smtp.login(sender,psd)
smtp.sendmail(sender,to_addrs,msg.as_string())
smtp.quit()
print('发送成功')
except Exception as result:
print('发送失败,异常为%s'%result) if __name__ == '__main__':
# make_excel()
send_eamil('xxx@xx.com')
python使用smtplib和email库发送邮件的更多相关文章
- Python自动发邮件——smtplib和email库和yagmail库
''' 一.先导入smtplib模块 导入MIMEText库用来做纯文本的邮件模板 二.发邮件几个相关的参数,每个邮箱的发件服务器不一样,以163为例子百度搜索服务器是 smtp.163.com 三. ...
- Python自动发送邮件-smtplib和email库
''' 一.先导入smtplib模块 导入MIMEText库用来做纯文本的邮件模板 二.发邮件几个相关的参数,每个邮箱的发件服务器不一样,以163为例子百度搜索服务器是 smtp.163.com 三. ...
- 利用Python的smtplib和email发送邮件
原理 网上已经有了很多的教程讲解相关的发送邮件的原理,在这里还是推荐一下廖雪峰老师的Python教程,讲解通俗易懂.简要来说,SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本 ...
- Python_使用smtplib和email模块发送邮件
[http://blog.csdn.net/menglei8625/article/details/7721746] SMTP (Simple Mail Transfer Protocol) 邮件传送 ...
- python使用smtplib和email发送腾讯企业邮箱邮件
公司每天要发送日报,最近没事搞了一下如何自动发邮件,用的是腾讯企业邮箱,跟大家分享一下我的研究过程吧. 以前弄的发邮件的是用qq邮箱发的,当时在网上查资料最后达到了能发图片,网页,自定义收件人展示,主 ...
- python email ==> send 发送邮件 :) [smtplib, email 模块]
关于Email的预备知识: 原贴地址:http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343480.html ############ ...
- Python自动发邮件-yagmail库
之前写过用标准库使用Python Smtplib和email发送邮件,感觉很繁琐,久了不用之后便忘记了.前几天看知乎哪些Python库让你相见恨晚?,看到了yagmail第三方库,学习过程中遇到一些问 ...
- python使用smtplib发送邮件
python要实现发送邮件的功能,需要使用smtplib库. 1. 过程大致如下: 1. 建立和SMTP邮件服务器的连接 # 默认端口25 smtp = smtplib.SMTP(host, port ...
- python:利用smtplib发送邮件详解
本文转自:https://www.cnblogs.com/insane-Mr-Li/p/9121619.html 自动化测试中,测试报告一般都需要发送给相关的人员,比较有效的一个方法是每次执行完测试用 ...
随机推荐
- LeetCode 047 Permutations II
题目要求:Permutations II Given a collection of numbers that might contain duplicates, return all possibl ...
- if判断 和while、for循环
if判断 语法一: if 条件: 条件成立时执行子代码块 代码1 代码2 实例一: sex='female' age=18 is_beautifui=True if sex=='female' ...
- Spring Boot 中使用 Spring Security, OAuth2 跨域问题 (自己挖的坑)
使用 Spring Boot 开发 API 使用 Spring Security + OAuth2 + JWT 鉴权,已经在 Controller 配置允许跨域: @RestController @C ...
- Spring Boot 2 集成 Swagger
本文测试代码使用 Spring Boot 2.1.6.RELEASE + Swagger 2.9.2 添加依赖 <dependency> <groupId>io.springf ...
- Zabbix监控使用进阶
1. Zabbix基于SNMP监控 1.1 zabbix-web所能指定的监控方式 ssh/telnet agent:master/agent SNMP:Simple Network Manageme ...
- PyQt(Python+Qt)学习随笔:窗口layout布局的SizeConstraint和部件的大小约束策略sizePolicy不起作用的问题
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 在写一个测试代码时,发现无论怎么设置窗口以及子部件 ...
- PyQt(Python+Qt)学习随笔:Designer中的QDialogButtonBox的StandardButtons标准按钮
在Qt Designer中,可以在界面中使用QDialogButtonBox来配置一组按钮进行操作,Qt中为QDialogButtonBox定义了一组常用的标准按钮,可以在Designer中直接在St ...
- jenkins+git部署环境,出现Failed to connect to repository : Command "git ls-remote -h http://gitlab.xxxxx.git HEAD" returned status code 128stdout: stderr: fatal: repository 'http://gitlab.xxxxx.git' not fou
1.部署jenkins+git源码管理的方式,源码管理报128stdout 源码管理出现如下错误: Failed to connect to repository : Command "gi ...
- 关于select下拉框选择触发事件
最开始使用onclick设置下拉框触发事件发现会有一些问题: <select> <option value="0" onclick="func0()&q ...
- Ubuntu18开机执行shell命令
1.打开shell终端,输入 sudo vi /etc/rc.local 2.在编辑器里面输入自己要启动的脚本,特别强调:脚本(程序)要有可执行权限 #!/bin/bash echo "ru ...