一、代码

# 输入邮件地址, 口令和POP3服务器地址:
import datetime
import email
import poplib
import email.policy
from email.parser import Parser
flag=False emailaddress = '******'
# 注意使用开通POP,SMTP等的授权码
password = '*******'
pop3_server = '*******' # 连接到POP3服务器:
server = poplib.POP3(pop3_server)
# 可以打开或关闭调试信息:
# server.set_debuglevel(1)
# POP3服务器的欢迎文字:
print(server.getwelcome()) # 身份认证:
server.user(self.emailaddress)
server.pass_(self.password) # stat()返回邮件数量和占用空间:
messagesCount, messagesSize = server.stat()
print('messagesCount:', messagesCount)
print('messagesSize:', messagesSize)
# list()返回所有邮件的编号:
resp, mails, octets = server.list()
print("resp:", resp)
print("mails:", mails)
print("octets:", octets) length = len(mails) print(length) for i in range(length):
i=length-i
resp, lines, octets = server.retr(i)
msg_content=b'\r\n'.join(lines)
msg = email.parser.BytesParser(policy=email.policy.default).parsebytes(msg_content)
print('发件人', msg['from'])
print('收件人', msg['to'])
print('主题', msg['subject'])
print('第一个收件人用户名', msg['to'].addresses[0].username)
print('第一个发件人用户名', msg['from'].addresses[0].username)
theme= msg['subject']
email_content=[part.get_content() for part in msg.walk() if part.get_content_maintype() == 'text'][0]
if theme=="启动查库任务分派机器人" and email_content==datetime.datetime.now().strftime("%Y%m%d"):
flag=True
break server.quit()
print(flag)

二、解决编码问题

import chardet

#继承重写解析类
class BytesParser_new(email.parser.BytesParser):
def parsebytes(self, text, headersonly=False):
import chardet
encoding=chardet.detect(text).get("encoding")
text = text.decode(encoding, errors='surrogateescape')
return self.parser.parsestr(text, headersonly) msg = BytesParser_new(policy=email.policy.default).parsebytes(msg_content)

三、代码封装

import email
import poplib
import email.policy class ReadEmail(object):
def __init__(self, emailaddress, password, pop3_server):
self.emailaddress = emailaddress
self.password = password
self.pop3_server = pop3_server
self.server = poplib.POP3(pop3_server) def login(self):
# 可以打开或关闭调试信息:
self.server.set_debuglevel(1)
# POP3服务器的欢迎文字:
print(self.server.getwelcome())
# 身份认证:
self.server.user(emailaddress)
self.server.pass_(password) def read(self):
self.login()
# stat()返回邮件数量和占用空间:
messagesCount, messagesSize = self.server.stat()
print('messagesCount:', messagesCount)
print('messagesSize:', messagesSize)
# list()返回所有邮件的编号:
resp, mails, octets = self.server.list()
print("resp:", resp)
print("mails:", mails)
print("octets:", octets)
length = len(mails)
print(length) for i in range(1, length):
resp, lines, octets = self.server.retr(i)
msg_content = b'\r\n'.join(lines)
msg = email.parser.BytesParser(policy=email.policy.default).parsebytes(msg_content)
# print('发件人', msg['from'])
# print('收件人', msg['to'])
print('主题', msg['subject'])
# print('第一个收件人用户名', msg['to'].addresses[0].username)
# print('第一个发件人用户名', msg['from'].addresses[0].username)
theme = msg['subject']
email_content = [part.get_content() for part in msg.walk() if part.get_content_maintype() == 'text'][0]
if theme == "系统退信/The email is returned":
self.server.dele(i)
print("删除邮件成功:", msg['subject']) self.server.quit() if __name__ == '__main__':
emailaddress = '********'
# 注意使用开通POP,SMTP等的授权码
password = '********'
pop3_server = 'imap.qq.com'
rm=ReadEmail(emailaddress,password ,pop3_server )
rm.read()

python实现收邮件判断模块poplib,email的更多相关文章

  1. python之模块poplib之常见用法

    # -*- coding: cp936 -*- #python 27 #xiaodeng #python之模块poplib之常见用法 ''' 所以,收取邮件分两步: 第一步:用poplib把邮件的原始 ...

  2. Python使用SMTP模块、email模块发送邮件

    一.smtplib模块: 主要通过SMTP类与邮件系统进行交互.使用方法如下: 1.实例化一个SMTP对象: s = smtplib.SMTP(邮件服务地址,端口号) s = smtplib.SMTP ...

  3. Python自动发邮件——smtplib和email库和yagmail库

    ''' 一.先导入smtplib模块 导入MIMEText库用来做纯文本的邮件模板 二.发邮件几个相关的参数,每个邮箱的发件服务器不一样,以163为例子百度搜索服务器是 smtp.163.com 三. ...

  4. Python通过yagmail和smtplib模块发送简单邮件

    SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件.python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是pytho ...

  5. 使用Python内置的smtplib包和email包来实现邮件的构造和发送。

    此文章github地址:https://github.com/GhostCNZ/Python_sendEmail Python_sendEmail 使用Python内置的smtplib包和email包 ...

  6. python发送各类邮件的主要方法

    更多详见: http://www.w3cschool.cc/python/python-email.html python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法 ...

  7. 九、Python发送QQ邮件(SMTP)

    看了廖雪峰老师的教程: 一封电子邮件的旅程就是 发件人 -> MUA -> MTA -> MTA -> 若干个MTA -> MDA <- MUA <- 收件人 ...

  8. Python之日志处理 logging模块

    Python之日志处理(logging模块)   本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logging模块日志流处理流程 使用logging四 ...

  9. Python 【收发邮件】

    发邮件 smtplib模块主要负责发送邮件 email模块主要负责构造邮件.这两个都是Python内置模块 smtplib.SMTP.方法 #按住Ctrl键并点击SMTP ,会看到对SMTP的解释(v ...

随机推荐

  1. hdu 6085 Rikka with Candies (set计数)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  2. CF 1045 H. Self-exploration 解题报告

    CF 1045 H. Self-exploration 考虑到串的结构一定是 1...0....1....0.....1... 这样的,而\(01\)与\(10\)在转折点交替出现 首先串长一定是\( ...

  3. 微信公众号ios10.1 版本白屏问题

    真机调试IOS 10.1.x的版本不支持fetch 所以一直loading显示白屏. 其他设备都没问题. 所以用主要属性window.fetch用来判断是否支持fetch 属性 import { ba ...

  4. sqlserver 找不到驱动,显示项目缺少class办法

    maven使用 <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId> ...

  5. python - 小米推送使用

    1. 小米文档及SDK下载 1.文档介绍 https://dev.mi.com/console/doc/detail?pId=863 sdk说明: 2.开发者需要登录开发者网站(申请AppID, Ap ...

  6. cannot find module node-sass

    解决方法: npm install --save-dev node-sass

  7. 转 Jmeter参数化--Post请求的Post body 参数化

    2018年01月22日 15:40:58 java2013liu 阅读数:2361收起 个人分类: Jemter   一.使用body data设置参数: 1,首先,使用Fiddler录制post请求 ...

  8. linux下vnstat查看服务器带宽流量统计

      因为很多vps或者服务器都是限流量的,但是又很多服务商并没有提供详细的流量表,比如每天的流量表,所以肯定有人很想知道自己服务器到底跑了多少流量. vnstat就是一个很好用的服务器流量统计命令.我 ...

  9. 14-python基础—python3中的defaultdict()

    1.collections.defaultdict 类 from collections import defaultdict 2.collections.defaultdict 类与工厂函数dict ...

  10. redis 入门之哈希

    hset 将哈希表 hash 中域 field 的值设置为 value .如果给定的哈希表并不存在, 那么一个新的哈希表将被创建并执行 HSET 操作.如果域 field 已经存在于哈希表中, 那么它 ...