iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >怎么在Python中对邮件进行处理
  • 247
分享到

怎么在Python中对邮件进行处理

2023-06-14 22:06:54 247人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

今天就跟大家聊聊有关怎么在python中对邮件进行处理,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。Python的五大特点是什么python的五大特点:1.简单易学,开发程序时,专注

今天就跟大家聊聊有关怎么在python中对邮件进行处理,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

Python的五大特点是什么

python的五大特点:1.简单易学,开发程序时,专注的是解决问题,而不是搞明白语言本身。2.面向对象,与其他主要的语言如c++和Java相比, Python以一种非常强大又简单的方式实现面向对象编程。3.可移植性,Python程序无需修改就可以在各种平台上运行。4.解释性,Python语言写的程序不需要编译成二进制代码,可以直接从源代码运行程序。5.开源,Python是 FLOSS(自由/开放源码软件)之一。

一、发送电子邮件

Python标准库提供了smtplib,用于实现SMTP协议发送邮件。标准库还提供email模块帮助我们构建邮件格式。SMTP(Simple Mail Transfer Protocol,即简单邮件传输协议),是一组有源地址到目的地址传送邮件的规则,用来控制信件的中转方式。

  • 获取QQ邮箱密码(授权码)

怎么在Python中对邮件进行处理 

二、发送纯文本格式的邮件

import smtplibfrom email.mime.text import MIMETextfrom email.header import Header# 邮箱用户名sender = 'dad@qq.com'(输入你的邮箱)# 邮箱密码(部分邮箱为授权码)passWord = '123456'(输入你的密码)# 收件人邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发receiver = ['baby@qq.com', ](输入你要发送人的邮箱)# 邮件正文text = 'Hello,baby'message = MIMEText(text, 'plain', 'utf-8')# 发件人显式的名字message['From'] = Header('拿头来坚持', 'utf-8')# 收件人显式的名字message['To'] = Header('baby', 'utf-8')# 邮件标题message['Subject'] = '爸爸来信,请接收!'try:    # 使用QQ企业邮箱服务器发送    smtp = smtplib.SMTP('smtp.qq.com')    # 登录    smtp.login(sender, password)    # 发送    smtp.sendmail(sender, receiver, message.as_string())    print('邮件发送成功!')    # 退出服务器    smtp.quit()except smtplib.SMTPException as e:    print('Error!邮件发送失败!', e)

发送纯文本格式的邮件执行结果:

怎么在Python中对邮件进行处理

三、发送html格式的邮件

import smtplibfrom email.mime.text import MIMETextfrom email.header import Header# 邮箱用户名sender = 'dad@qq.com'(输入你的邮箱)# 邮箱密码(部分邮箱为授权码)password = '123456'(输入你的密码)# 收件人邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发receiver = ['baby@qq.com', ](输入你要发送人的邮箱)# 邮件正文msg = '''    <p><a href='https://blog.csdn.net/weixin_46382560?spm=1011.2124.3001.5343'>拿头来坚持</p>          Life Goes on, learning goes on        </p> <!----></div></div> <div class="user-profile-head-info-b" data-v-d1dbb6f8><ul data-v-d1dbb6f8><li data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>22,574</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>被访问量</div></li> <li data-v-d1dbb6f8><a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>24</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>原创文章</div></a></li> <li data-v-d1dbb6f8><a href="Https://blog.csdn.net/rank/list/total" rel="external nofollow"  rel="external nofollow"  target="_blank" data-report-click="{&quot;spm&quot;:&quot;3001.5476&quot;}" data-report-query="spm=3001.5476" data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>128,997</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>作者排名</div></a></li> <li data-v-d1dbb6f8><a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>762</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>粉丝数量</div></a></li></ul></div></div></div> <div class="user-profile-body" data-v-3f0fdf46 data-v-80922f46><div class="user-profile-body-inner" data-v-3f0fdf46><div class="user-profile-body-left" data-v-3f0fdf46><div class="user-profile-aside" data-v-d487ed78 data-v-3f0fdf46><div class="user-general-info single-general-info" data-v-d487ed78><ul data-v-d487ed78><!----> <!----> <li class="user-general-info-join-csdn" data-v-d487ed78><i data-v-d487ed78></i> <span data-v-d487ed78>于</span> <span class="user-general-info-key-word" data-v-d487ed78>2020-02-22</span> <span data-v-d487ed78>加入CSDN</span></li></ul></div> <!----> <div class="user-achievement user-profile-aside-common-box" data-v-d487ed78><div class="aside-common-box-head" data-v-d487ed78>获得成就</div> <div class="aside-common-box-bottom" data-v-d487ed78><div class="aside-common-box-content" data-v-d487ed78><ul data-v-d487ed78><li data-v-d487ed78>        <i ></i>        <div>获得<span>212</span>次点赞</div>      </li><li data-v-d487ed78>        <i ></i>        <div>内容获得<span>111</span>次评论</div>      </li><li data-v-d487ed78>        <i ></i>        <div>获得<span>562</span>次收藏</div>      '''# 指定消息体使用HTML格式message = MIMEText(msg, 'html', 'utf-8')# 发件人显式的名字message['From'] = Header('拿头来坚持', 'utf-8')# 收件人显式的名字message['To'] = Header('baby', 'utf-8')# 邮件标题message['Subject'] = '爸爸来信,请接收!'try:    # 使用QQ企业邮箱服务器发送    smtp = smtplib.SMTP('smtp.qq.com')    # 登录    smtp.login(sender, password)    # 发送    smtp.sendmail(sender, receiver, message.as_string())    print('邮件发送成功!')    # 退出服务器    smtp.quit()except smtplib.SMTPException as e:    print('Error!邮件发送失败!', e)

发送HTML格式的邮件的执行结果:

怎么在Python中对邮件进行处理

四、发送带附件的邮件

import smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartfrom email.header import Header# 邮箱用户名sender = 'dad@qq.com'(输入你的邮箱)# 邮箱密码(部分邮箱为授权码)password = '123456'(输入你的密码)# 收件人邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发receiver = ['baby@qq.com', ](输入你要发送人的邮箱)# 指定消息体使用复合类型message = MIMEMultipart()# 发件人显式的名字message['From'] = Header('拿头来坚持', 'utf-8')# 收件人显式的名字message['To'] = Header('baby', 'utf-8')# 邮件标题message['Subject'] = '爸爸来信,请接收!'# 邮件正文msg = '''    <p><a href='https://blog.csdn.net/weixin_46382560?spm=1011.2124.3001.5343'>拿头来坚持</p>          Life goes on, learning goes on        </p> <!----></div></div> <div class="user-profile-head-info-b" data-v-d1dbb6f8><ul data-v-d1dbb6f8><li data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>22,574</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>被访问量</div></li> <li data-v-d1dbb6f8><a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>24</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>原创文章</div></a></li> <li data-v-d1dbb6f8><a href="https://blog.csdn.net/rank/list/total" rel="external nofollow"  rel="external nofollow"  target="_blank" data-report-click="{&quot;spm&quot;:&quot;3001.5476&quot;}" data-report-query="spm=3001.5476" data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>128,997</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>作者排名</div></a></li> <li data-v-d1dbb6f8><a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  data-v-d1dbb6f8><div class="user-profile-statistics-num" data-v-d1dbb6f8>762</div> <div class="user-profile-statistics-name" data-v-d1dbb6f8>粉丝数量</div></a></li></ul></div></div></div> <div class="user-profile-body" data-v-3f0fdf46 data-v-80922f46><div class="user-profile-body-inner" data-v-3f0fdf46><div class="user-profile-body-left" data-v-3f0fdf46><div class="user-profile-aside" data-v-d487ed78 data-v-3f0fdf46><div class="user-general-info single-general-info" data-v-d487ed78><ul data-v-d487ed78><!----> <!----> <li class="user-general-info-join-csdn" data-v-d487ed78><i data-v-d487ed78></i> <span data-v-d487ed78>于</span> <span class="user-general-info-key-word" data-v-d487ed78>2020-02-22</span> <span data-v-d487ed78>加入CSDN</span></li></ul></div> <!----> <div class="user-achievement user-profile-aside-common-box" data-v-d487ed78><div class="aside-common-box-head" data-v-d487ed78>获得成就</div> <div class="aside-common-box-bottom" data-v-d487ed78><div class="aside-common-box-content" data-v-d487ed78><ul data-v-d487ed78><li data-v-d487ed78>        <i ></i>        <div>获得<span>212</span>次点赞</div>      </li><li data-v-d487ed78>        <i ></i>        <div>内容获得<span>111</span>次评论</div>      </li><li data-v-d487ed78>        <i ></i>        <div>获得<span>562</span>次收藏</div>      '''# 邮件附加html文件message.attach(MIMEText(msg, 'html', 'utf-8'))# 添加附件attached_file = MIMEText(open(__file__, encoding='utf-8').read(), 'base64', 'utf-8')# 指定附件的文件名和原先的文件不一样attached_file['Content-Disposition'] = 'attachment;filename="mail.py"'# 邮件附加附件message.attach(attached_file)try:    # 使用QQ企业邮箱服务器发送    smtp = smtplib.SMTP('smtp.qq.com')    # 登录    smtp.login(sender, password)    # 发送    smtp.sendmail(sender, receiver, message.as_string())    print('邮件发送成功!')    # 退出服务器    smtp.quit()except smtplib.SMTPException as e:    print('Error!邮件发送失败!', e)

五、发送图片的邮件

import smtplibfrom email.mime.text import MIMETextfrom email.mime.image import MIMEImagefrom email.mime.multipart import MIMEMultipartfrom email.header import Header# 邮箱用户名sender = 'dad@qq.com'(输入你的邮箱)# 邮箱密码(部分邮箱为授权码)password = '123456'(输入你的密码)# 收件人邮箱地址,注意需要[]包裹,这意味着你可以写多个邮件地址群发receiver = ['baby@qq.com', ](输入你要发送人的邮箱)# 利用related定义内嵌资源的邮件体message = MIMEMultipart('related')# 发件人显式的名字message['From'] = Header('拿头来坚持', 'utf-8')# 收件人显式的名字message['To'] = Header('baby', 'utf-8')# 邮件标题message['Subject'] = '爸爸来信,请接收!'# 邮件正文content = MIMEMultipart('alternative')# html内容msg = '''    <p><a href='https://blog.csdn.net/weixin_46382560?spm=1011.2124.3001.5343'>拿头来坚持</p>          Life goes on, learning goes on    <p>    拿头来坚持的个人主页    <img src='cid:img01'>    </p>      '''# 邮件附加html文件message.attach(MIMEText(msg, 'html', 'utf-8'))# 添加图片with open('csdn.png', 'rb') as f:    img01 = MIMEImage(f.read())# 定义资源的名字为img01img01.add_header('Content-ID', 'img01')# 邮件附加图片message.attach(img01)try:    # 使用QQ企业邮箱服务器发送    smtp = smtplib.SMTP('smtp.qq.com')    # 登录    smtp.login(sender, password)    # 发送    smtp.sendmail(sender, receiver, message.as_string())    print('邮件发送成功!')    # 退出服务器    smtp.quit()except smtplib.SMTPException as e:    print('Error!邮件发送失败!', e)

发送图片的邮件执行结果:

怎么在Python中对邮件进行处理

六、接收电子邮件

接受邮件有两种常用的协议:POP3和IMAP协议

POP3协议(Post Office Protocol-Version3,即邮局协议版本3):允许电子邮件客户端下载服务器上的邮件,但是在客户端的操作(如移动邮件、标记已读等)不会反馈到服务器上,比如通过客户端收取了邮箱的3封邮件并移动到其他文件夹,邮件服务器上的这些邮件不会被同步移动。

IMAP协议(Internet Mail Access Protocol,即Internet邮件访问协议):提供WEBmail与电子邮件客户端之间的双向通信,任何在客户端做的改变都会同步到服务器上。在客户端对邮件进行了操作,服务器上的邮件也会进行相应的操作。

七、使用POP3协议下载邮件

import poplibfrom email.parser import Parser# 登录邮箱的用户名username = 'baby@qq.com'(输入你的邮箱)# 登录邮箱的密码(部分邮箱为授权码)password = '123456'(输入你的密码)# 连接邮箱服务器pop_server = poplib.POP3('pop.qq.com')# 打印出邮箱服务器的欢迎文字print(pop_server.getwelcome())# 登录邮箱服务器pop_server.user(username)pop_server.pass_(password)# 打印出当前账号的状态,第一个返回值为邮件数,第二个返回值为占用空间print('Server stat', pop_server.stat())# 获取所以邮件列表resp, mails, octets = pop_server.list()print(mails)# 获取最新的一封邮件(序列号最大的),邮件索引从1开始计数index = len(mails)resp, lines, octets = pop_server.retr(index)content = b'\r\n'.join(lines).decode('utf-8')# 解析出邮件msg = Parser().parsestr(content)# 可以根据邮件索引号直接从服务器删除邮件# pop_server.dele(index)# 关闭连接pop_server.quit()

执行结果:

b'+OK XMail POP3 Server v1.0 Service Ready(XMail v1.0)'
Server stat (15, 50814)
[b'1 1255', b'2 1286', b'3 1310', b'4 1398', b'5 1458', b'6 1450', b'7 1602', b'8 1633', b'9 5001', b'10 2347', b'11 2371', b'12 2267', b'13 5033', b'14 5077', b'15 17326']

看完上述内容,你们对怎么在Python中对邮件进行处理有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网Python频道,感谢大家的支持。

--结束END--

本文标题: 怎么在Python中对邮件进行处理

本文链接: https://www.lsjlt.com/news/275383.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么在Python中对邮件进行处理
    今天就跟大家聊聊有关怎么在Python中对邮件进行处理,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。python的五大特点是什么python的五大特点:1.简单易学,开发程序时,专注...
    99+
    2023-06-14
  • 怎么在Python中对Args进行处理
    这篇文章将为大家详细讲解有关怎么在Python中对Args进行处理,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。1. sys 模块Python 中的 sys 模块具有 argv 功能。当通过...
    99+
    2023-06-14
  • Python中怎么对文件进行处理
    本篇文章给大家分享的是有关Python中怎么对文件进行处理,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。建议一:使用 pathlib 模块如果你需要在 Python 里进行文件...
    99+
    2023-06-15
  • 怎么在python中中对信号进行处理
    今天就跟大家聊聊有关怎么在python中中对信号进行处理,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。python可以做什么Python是一种编程语言,内置了许多有效的工具,Pyth...
    99+
    2023-06-14
  • 怎么在python中对图像进行灰度处理
    本篇文章给大家分享的是有关怎么在python中对图像进行灰度处理,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。Python的优点有哪些1、简单易用,与C/C++、Java、C#...
    99+
    2023-06-14
  • Python中怎么邮件处理
    Python中怎么邮件处理,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。一、发送电子邮件Python标准库提供了smtplib,用于实现SMTP协议发送邮件。标准库还提供e...
    99+
    2023-06-20
  • 怎么在pandas apply中对并行进行处理
    怎么在pandas apply中对并行进行处理?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。1. pandarallel (pip install )对于一个带有Panda...
    99+
    2023-06-06
  • 怎么在python中使用moviepy对视频进行处理
    本篇文章为大家展示了怎么在python中使用moviepy对视频进行处理,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。Python主要用来做什么Python主要应用于:1、Web开发;2、数据科学研...
    99+
    2023-06-08
  • Python中怎么批量对文件进行压缩处理
    本篇文章为大家展示了Python中怎么批量对文件进行压缩处理,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。1、第一步导入需要的三个库import os as os&nb...
    99+
    2023-06-15
  • 怎么在python中使用except对异常进行处理
    本篇文章为大家展示了怎么在python中使用except对异常进行处理,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。python有哪些常用库python常用的库:1.requesuts;2.scra...
    99+
    2023-06-14
  • 怎么在css中对空格进行处理
    今天就跟大家聊聊有关怎么在css中对空格进行处理,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。1、空格规则HTML 代码的空格通常会被浏览器忽略。<p> &nb...
    99+
    2023-06-08
  • Python-对Pcap文件进行处理,获
            通过对TCP/IP协议的学习,本人写了一个可以实现对PCAP文件中的IPV4下的TCP流提取,以及提取指定的TCP流,鉴于为了学习,没有采用第三方包解析pcap,而是对bytes流进行解析,其核心思想为:若想要提取TCP ...
    99+
    2023-01-30
    文件 Python Pcap
  • 怎么在shell中对长命令进行换行处理
    怎么在shell中对长命令进行换行处理?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。前言考察下面的脚本:emcc -o ./dist/tes...
    99+
    2023-06-09
  • 怎么在pygame中对键盘和鼠标事件进行处理
    本篇文章为大家展示了怎么在pygame中对键盘和鼠标事件进行处理,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。pygame 的常用事件如下所示:事件产生途径参数QUIT用户按下“关闭”按钮NoneA...
    99+
    2023-06-14
  • Python中怎么实现邮件处理
    这篇文章给大家介绍Python中怎么实现邮件处理,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。一、发送电子邮件Python标准库提供了smtplib,用于实现SMTP协议发送邮件。标准库还提供email模块帮助我们构建...
    99+
    2023-06-20
  • 使用Python怎么对Excel进行处理
    本篇文章为大家展示了使用Python怎么对Excel进行处理,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。python是什么意思Python是一种跨平台的、具有解释性、编译性、互动性和面向对象的脚本...
    99+
    2023-06-07
  • python怎么对csv数据进行处理
    Python中可以使用csv模块来对csv数据进行处理。以下是一个示例代码,演示了如何读取csv文件、写入csv文件以及对数据进行处...
    99+
    2024-02-29
    python
  • Android中怎么对cookie进行处理
    本篇文章给大家分享的是有关Android中怎么对cookie进行处理,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。android 客户端 Cookie处理Cookie,有时也用...
    99+
    2023-05-30
    android cookie
  • Node.js中怎么对CORS进行处理
    今天就跟大家聊聊有关Node.js中怎么对CORS进行处理,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。什么是CORSCORS 是“跨域资源共享”的...
    99+
    2024-04-02
  • 怎么在Java项目中对异常进行处理
    本文章向大家介绍怎么在Java项目中对异常进行处理的基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。Java可以用来干什么Java主要应用于:1. web开发;2. Android开发;3. 客户端开发;4. 网页...
    99+
    2023-06-06
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作