简体   繁体   English

使用poplib,Python获取未读消息

[英]Getting UnRead Messages using poplib, Python

I'm trying to write some script to check a mail service which uses pop3. 我正在尝试编写一些脚本来检查使用pop3的邮件服务。 As you can see I use poplib module. 如您所见,我使用poplib模块。 However, I don't see the correct way to get the unread messages. 但是,我看不到获取未读邮件的正确方法。 I found a method which retrieves all the mails so it takes so much time in this call: 我找到了一种检索所有邮件的方法,因此在此调用中需要花费大量时间:

messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]

Is there a way to filter the unread messages? 有没有办法过滤未读消息? Do you know any other good module to deal with pop mail service? 您是否知道其他处理流行邮件服务的好模块? Here is the code I'm using: 这是我正在使用的代码:

import poplib
from email import parser
pop_conn = poplib.POP3_SSL('pop3.live.com', 995) #Connect to hotmail pop3 server
pop_conn.user('address')
pop_conn.pass_('password')
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ["\n".join(mssg[1]) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
    print "{} : {}\n".format(message['subject'], message['From'])
pop_conn.quit()

There's a workaround. 有一种解决方法。

You can use message_count, mailbox_size = pop_conn.stat() to retrieve message_count and store somehow. 您可以使用message_count, mailbox_size = pop_conn.stat()来检索message_count并以某种方式存储。 Later, You can call again pop_conn.stat() and compare message_count with the stored one (remember to update the stored value if it changes). 稍后,您可以再次调用pop_conn.stat()并将message_count与存储的值进行比较(请记住,如果存储的值发生更改,则更新存储的值)。 The difference is the "unread" mails. 区别在于“未读”邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM