本文主要是介绍通过POP3协议读取指定邮件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
先要安装maillib库
假设我要获得一周来,豆瓣发送的每个邮件中的第一个网址
import poplib
import maillib
from datetime import datetime, timedeltadef email_filter(sender, body):target = "http://"sender, body = sender[1], body.split("\n")if sender == "webmaster@douban.com":for line in body:if target in line:return line.strip()def read_email(email, password, host, port=110, days=0):conn = poplib.POP3(host, port)#conn.set_debuglevel(1) #输出调试信息conn.user(email)conn.pass_(password)links = []nr = conn.stat()[0] #获取邮件数量for i in range(nr, 0, -1):server_msg, body, octets = conn.retr(i)msg = maillib.Message.from_string( "\n".join(body) )today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)if msg.date < today - timedelta(days=days):breaklink = email_filter(msg.sender, msg.body)if link:links.append(link)return linksif __name__ == "__main__":links = read_email("me@126.com", "pass", host="pop.126.com", days=7)for link in links:print link
这篇关于通过POP3协议读取指定邮件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!