python实现收邮件判断模块poplib,email
一、代码
# 输入邮件地址, 口令和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的更多相关文章
- python之模块poplib之常见用法
# -*- coding: cp936 -*- #python 27 #xiaodeng #python之模块poplib之常见用法 ''' 所以,收取邮件分两步: 第一步:用poplib把邮件的原始 ...
- Python使用SMTP模块、email模块发送邮件
一.smtplib模块: 主要通过SMTP类与邮件系统进行交互.使用方法如下: 1.实例化一个SMTP对象: s = smtplib.SMTP(邮件服务地址,端口号) s = smtplib.SMTP ...
- Python自动发邮件——smtplib和email库和yagmail库
''' 一.先导入smtplib模块 导入MIMEText库用来做纯文本的邮件模板 二.发邮件几个相关的参数,每个邮箱的发件服务器不一样,以163为例子百度搜索服务器是 smtp.163.com 三. ...
- Python通过yagmail和smtplib模块发送简单邮件
SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件.python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是pytho ...
- 使用Python内置的smtplib包和email包来实现邮件的构造和发送。
此文章github地址:https://github.com/GhostCNZ/Python_sendEmail Python_sendEmail 使用Python内置的smtplib包和email包 ...
- python发送各类邮件的主要方法
更多详见: http://www.w3cschool.cc/python/python-email.html python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法 ...
- 九、Python发送QQ邮件(SMTP)
看了廖雪峰老师的教程: 一封电子邮件的旅程就是 发件人 -> MUA -> MTA -> MTA -> 若干个MTA -> MDA <- MUA <- 收件人 ...
- Python之日志处理 logging模块
Python之日志处理(logging模块) 本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logging模块日志流处理流程 使用logging四 ...
- Python 【收发邮件】
发邮件 smtplib模块主要负责发送邮件 email模块主要负责构造邮件.这两个都是Python内置模块 smtplib.SMTP.方法 #按住Ctrl键并点击SMTP ,会看到对SMTP的解释(v ...
随机推荐
- 基于c语言数据结构+严蔚敏——线性表章节源码,利用Codeblocks编译通过
白天没屌事,那我们就来玩玩线性表的实现吧,快要失业了,没饭吃了咋整哦 题目描述假设利用两个线性表LA和LB分别表示两个集合A和B(即:线性表中的数据元素即为集合中的成员),现要求一个新的集合A=A∪B ...
- [CSP-S模拟测试]:E(贪心)
题目传送门(内部题48) 输入格式 第一行一个整数$n$.接下来$n$行每行两个整数$x_i,y_i$. 输出格式 一行一个整数表示答案. 样例 样例输入$1$: 23 72 5 样例输出$1$: 样 ...
- c 语言 volatile 关键字
一.前言 1.编译器优化介绍: 由于内存访问速度远不及CPU处理速度,为提高机器整体性能,在硬件上引入硬件高速缓存Cache,加速对内存的访问.另外在现代CPU中指令的执行并不一定严格按照顺序执行,没 ...
- ROS录制主题和放
1.按照指定名称录制指定主题, 如录制主题为line_markers,名称为line_extraction的bag包. rosbag record -O line_extraction.bag /li ...
- HTML5: HTML5 服务器发送事件(Server-Sent Events)
ylbtech-HTML5: HTML5 服务器发送事件(Server-Sent Events) 1.返回顶部 1. HTML5 服务器发送事件(Server-Sent Events) HTML5 服 ...
- 94、tensorflow实现语音识别0,1,2,3,4,5,6,7,8,9
''' Created on 2017年7月23日 @author: weizhen ''' #导入库 from __future__ import division,print_function,a ...
- Ettercap详细参数
关于界面:ettercap提供 4 种运行界面: Text #文本模式,参数 -T ,一般配合 -q(安静模式)使用 Curses/GTK #图形模式,参数 -C ...
- 2019ccpc网络赛hdu6703 array(线段树)
array 题目传送门 解题思路 操作1是把第pos个位置上的数加上\(10^7\),操作2是找到区间[1,r]中没有且大于k的最小的数.注意到k的范围是小于等于n的,且n的范围是\(10^5\),远 ...
- css3水平垂直居中(不知道宽高同样适用)
css水平垂直居中 第一种方法: 在父div里加: display: table-cell; vertical-align: middle; text-align: center; 内部div设置: ...
- PAT甲级——A1152 GoogleRecruitment【20】
In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...