python发送邮件 python发送qq,163,sohu, xinlang, 126等邮件 python自动发邮件总结及实例说明...
python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是python自带的,只需import即可使用。smtplib模块主要负责发送邮件,email模块主要负责构造邮件。
smtplib模块主要负责发送邮件:是一个发送邮件的动作,连接邮箱服务器,登录邮箱,发送邮件(有发件人,收信人,邮件内容)。
email模块主要负责构造邮件:指的是邮箱页面显示的一些构造,如发件人,收件人,主题,正文,附件等。
1.smtplib模块
smtplib使用较为简单。以下是最基本的语法。
导入及使用方法如下:
import smtplibsmtp = smtplib.SMTP() smtp.connect('smtp.163.com',25) smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) smtp.quit()
说明:
smtplib.SMTP():实例化SMTP()
connect(host,port):
host:指定连接的邮箱服务器。常用邮箱的smtp服务器地址如下:
新浪邮箱:smtp.sina.com,新浪VIP:smtp.vip.sina.com,搜狐邮箱:smtp.sohu.com,126邮箱:smtp.126.com,139邮箱:smtp.139.com,163网易邮箱:smtp.163.com。
port:指定连接服务器的端口号,默认为25.
login(user,password):
user:登录邮箱的用户名。
password:登录邮箱的密码,像笔者用的是网易邮箱,网易邮箱一般是网页版,需要用到客户端密码,需要在网页版的网易邮箱中设置授权码,该授权码即为客户端密码。
sendmail(from_addr,to_addrs,msg,...):
from_addr:邮件发送者地址
to_addrs:邮件接收者地址。字符串列表['接收地址1','接收地址2','接收地址3',...]或'接收地址'
msg:发送消息:邮件内容。一般是msg.as_string():as_string()是将msg(MIMEText对象或者MIMEMultipart对象)变为str。
quit():用于结束SMTP会话。
2.email模块
email模块下有mime包,mime英文全称为“Multipurpose Internet Mail Extensions”,即多用途互联网邮件扩展,是目前互联网电子邮件普遍遵循的邮件技术规范。
该mime包下常用的有三个模块:text,image,multpart。
导入方法如下:
from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage
构造一个邮件对象就是一个Message
对象,如果构造一个MIMEText
对象,就表示一个文本邮件对象,如果构造一个MIMEImage
对象,就表示一个作为附件的图片,要把多个对象组合起来,就用MIMEMultipart
对象,而MIMEBase
可以表示任何对象。它们的继承关系如下:
Message
+- MIMEBase+- MIMEMultipart+- MIMENonMultipart+- MIMEMessage+- MIMEText+- MIMEImage
2.1 text说明
邮件发送程序为了防止有些邮件阅读软件不能显示处理HTML格式的数据,通常都会用两类型分别为"text/plain"和"text/html"
构造MIMEText对象时,第一个参数是邮件正文,第二个参数是MIME的subtype,最后一定要用utf-8编码保证多语言兼容性。
2.1.1添加普通文本
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.baidu.com" text_plain = MIMEText(text,'plain', 'utf-8')
查看MIMEText属性:可以观察到MIMEText,MIMEImage和MIMEMultipart的属性都一样。
print dir(text_plain)
['__contains__', '__delitem__', '__doc__', '__getitem__', '__init__', '__len__', '__module__', '__setitem__', '__str__', '_charset', '_default_type', '_get_params_preserve', '_headers', '_payload', '_unixfrom', 'add_header', 'as_string', 'attach', 'defects', 'del_param', 'epilogue', 'get', 'get_all', 'get_boundary', 'get_charset', 'get_charsets', 'get_content_charset', 'get_content_maintype', 'get_content_subtype', 'get_content_type', 'get_default_type', 'get_filename', 'get_param', 'get_params', 'get_payload', 'get_unixfrom', 'has_key', 'is_multipart', 'items', 'keys', 'preamble', 'replace_header', 'set_boundary', 'set_charset', 'set_default_type', 'set_param', 'set_payload', 'set_type', 'set_unixfrom', 'values', 'walk']
2.1.2添加超文本
html = """ <html> <body> <p> Here is the <a href="http://www.baidu.com">link</a> you wanted.</p> </body> </html> """ text_html = MIMEText(html,'html', 'utf-8')
2.1.3添加附件
sendfile=open(r'D:\pythontest\1111.txt','rb').read() text_att = MIMEText(sendfile, 'base64', 'utf-8') text_att["Content-Type"] = 'application/octet-stream' text_att["Content-Disposition"] = 'attachment; filename="显示的名字.txt"'
2.2 image说明
添加图片:
sendimagefile=open(r'D:\pythontest\testimage.png','rb').read() image = MIMEImage(sendimagefile) image.add_header('Content-ID','<image1>')
查看MIMEImage属性:
print dir(image)
['__contains__', '__delitem__', '__doc__', '__getitem__', '__init__', '__len__', '__module__', '__setitem__', '__str__', '_charset', '_default_type', '_get_params_preserve', '_headers', '_payload', '_unixfrom', 'add_header', 'as_string', 'attach', 'defects', 'del_param', 'epilogue', 'get', 'get_all', 'get_boundary', 'get_charset', 'get_charsets', 'get_content_charset', 'get_content_maintype', 'get_content_subtype', 'get_content_type', 'get_default_type', 'get_filename', 'get_param', 'get_params', 'get_payload', 'get_unixfrom', 'has_key', 'is_multipart', 'items', 'keys', 'preamble', 'replace_header', 'set_boundary', 'set_charset', 'set_default_type', 'set_param', 'set_payload', 'set_type', 'set_unixfrom', 'values', 'walk']
2.3 multpart说明
常见的multipart类型有三种:multipart/alternative, multipart/related和multipart/mixed。
邮件类型为"multipart/alternative"的邮件包括纯文本正文(text/plain)和超文本正文(text/html)。
邮件类型为"multipart/related"的邮件正文中包括图片,声音等内嵌资源。
邮件类型为"multipart/mixed"的邮件包含附件。向上兼容,如果一个邮件有纯文本正文,超文本正文,内嵌资源,附件,则选择mixed类型。
msg = MIMEMultipart('mixed')
我们必须把Subject,From,To,Date添加到MIMEText对象或者MIMEMultipart对象中,邮件中才会显示主题,发件人,收件人,时间(若无时间,就默认一般为当前时间,该值一般不设置)。
msg = MIMEMultipart('mixed') msg['Subject'] = 'Python email test' msg['From'] = 'XXX@163.com <XXX@163.com>' msg['To'] = 'XXX@126.com' msg['Date']='2012-3-16'
查看MIMEMultipart属性:
msg = MIMEMultipart('mixed') print dir(msg)
结果:
['__contains__', '__delitem__', '__doc__', '__getitem__', '__init__', '__len__', '__module__', '__setitem__', '__str__', '_charset', '_default_type', '_get_params_preserve', '_headers', '_payload', '_unixfrom', 'add_header', 'as_string', 'attach', 'defects', 'del_param', 'epilogue', 'get', 'get_all', 'get_boundary', 'get_charset', 'get_charsets', 'get_content_charset', 'get_content_maintype', 'get_content_subtype', 'get_content_type', 'get_default_type', 'get_filename', 'get_param', 'get_params', 'get_payload', 'get_unixfrom', 'has_key', 'is_multipart', 'items', 'keys', 'preamble', 'replace_header', 'set_boundary', 'set_charset', 'set_default_type', 'set_param', 'set_payload', 'set_type', 'set_unixfrom', 'values', 'walk']
说明:
msg.add_header(_name,_value,**_params):添加邮件头字段。
msg.as_string():是将msg(MIMEText对象或者MIMEMultipart对象)变为str,如果只有一个html超文本正文或者plain普通文本正文的话,一般msg的类型可以是MIMEText;如果是多个的话,就都添加到MIMEMultipart,msg类型就变为MIMEMultipart。
msg.attach(MIMEText对象或MIMEImage对象):将MIMEText对象或MIMEImage对象添加到MIMEMultipart对象中。MIMEMultipart对象代表邮件本身,MIMEText对象或MIMEImage对象代表邮件正文。
以上的构造的文本,超文本,附件,图片都何以添加到MIMEMultipart('mixed')中:
msg.attach(text_plain) msg.attach(text_html) msg.attach(text_att) msg.attach(image)
3.文字,html,图片,附件实现实例
(尊重笔者的劳动哦,转载请说明出处哦。)
参考
python自动发邮件总结及实例说明
python自动发邮件总结及实例说明
https://www.cnblogs.com/yufeihlf/p/5726619.html
转载于:https://www.cnblogs.com/stillstep/p/11219729.html
python发送邮件 python发送qq,163,sohu, xinlang, 126等邮件 python自动发邮件总结及实例说明...相关推荐
- python判断邮件发送成功_【基本解决】python中用SMTP发送QQ邮件提示成功但是收件人收不到邮件...
折腾: 期间, 已经用了smtp的ssl去发送邮件了,但是结果: 第二收件人也没有收到邮件... 那去把端口号从465改为587: smtpPort=587, 结果直接出错: smtpObj = sm ...
- oracle 发的邮件qq,python基于SMTP发送QQ邮件
python基于SMTP发送QQ邮件,供大家参考,具体内容如下 第一步: 1.打开qq邮箱,开启smtp服务 2.验证密保 3.获取授权码 python代码里面使用 在Python中, smtplib ...
- python qq群自动加入_Python实现向QQ群成员自动发邮件的方法
本文实例讲述了Python实现向QQ群成员自动发邮件的方法.分享给大家供大家参考.具体实现方法如下: 原理: 我们需要先获取QQ群中的所有成员并保存到一个txt文本中去,然后再由python读取文件然 ...
- python自动发送qq群消息_Python实现向QQ群成员自动发邮件的方法
本文实例讲述了Python实现向QQ群成员自动发邮件的方法.分享给大家供大家参考.具体实现方法如下: 原理: 我们需要先获取QQ群中的所有成员并保存到一个txt文本中去,然后再由python读取文件然 ...
- python脚本自动发邮件功能
最近给项目加监控,用python写了一个脚本,在出错的时候自动发邮件通知的功能.代码如下 #! /usr/bin/python # -*- coding: utf-8 -*- from smtplib ...
- 通过Python自动发邮件《生如夏花》
今天ajupyter和姐姐出去逛了一天街,累死了.晚上读了一首非常美丽的诗词,是泰戈尔的<生如夏花>,感觉非常美,再加上前几天学会了用python自动发邮件,决定把这首诗发给自己的好朋友欣 ...
- html+自动发送邮件,windows下BLAT程序SMTP自动发邮件教程(支持html)
------------------------ Blat - A Windows (32 & 64 bit) command line SMTP mailer. 一款支持windows cm ...
- python自动发outlook邮件_使用python自动发邮件(163、QQ、outlook邮箱)
import smtplib fromemail.mime.text import MIMEText fromemail.utils import formataddr fromdatetime im ...
- Python 发送邮件 和 发送带附件邮件
摘要: 平时运行一些脚本,需要把结果发送到邮箱,可以用python来处理,如下: 1,有邮件服务器: #!/bin/env python # -*- encoding: utf-8 -*- ...
最新文章
- 1.5亿美元!英国AI芯片创企今宣布获新融资,估值近20亿美元
- java之filter原理与责任链模式
- 强化学习之三:双臂赌博机(Two-armed Bandit)
- linux xia 安装程序,linux更新或安装libzip
- ML.NET Cookbook:(11)如果我的训练数据不在文本文件中怎么办?
- java 读取1m文件_java的FileInputStream类读取文件
- js检查元素是否包括在数组中
- NO.34 关于禅道加密
- javascript之调度:setTimeout 和 setInterval
- poj 2417 Discrete Logging
- Azure 基础:Blob Storage
- 简述hdfs工作原理_简述HDFS的原理?
- 软件开发模型、瀑布模型、V模型、原型模型、增量模型、螺旋模型、喷泉模型
- 联想MIIX510 黑苹果教程 整机升级
- 弱口令扫描工具mysql ftp_S-X弱口令扫描工具 V1.0
- Unity学习笔记:Rule Tile、Advance Rule Overide Tile、Rule Override Tile的用法【By Chutianto】
- 大型互联网网站割接方案
- 笔记本wifi热点设置
- python-Opencv 图片锐化
- 数组:605.种花问题