首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Python和poplib获取电子邮件

使用Python和poplib获取电子邮件
EN

Stack Overflow用户
提问于 2011-12-29 22:40:46
回答 3查看 40.5K关注 0票数 9

我想用Python登录我的帐户,让python打印我在邮箱中收到的消息。我知道怎么联系

代码语言:javascript
复制
import getpass, poplib
user = 'my_user_name' 
Mailbox = poplib.POP3_SSL('pop.googlemail.com', '995') 
Mailbox.user(user) 
Mailbox.pass_('my_password') 

我不知道如何让Python显示我的消息。我尝试了poplib文档中的所有函数。它们只显示数字。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-12-29 23:05:03

您还没有发布您的源代码,但这是我的回应:

如何获取消息总数:

代码语言:javascript
复制
(numMsgs, totalSize) = self.conn_pop3.stat()

如何在知道邮箱中的号码的情况下获取特定消息:

代码语言:javascript
复制
(server_msg, body, octets) = self.conn_pop3.retr(number)

所以你可能需要的函数是,retr,,它返回一个元组。参见here

注意,它还设置了在服务器上看到的相应电子邮件!您可能可以撤消该操作,至少使用IMAP可以。

我的pop3库电子邮件实现是这样的:

代码语言:javascript
复制
from poplib  import POP3
...
    if self.pop3_connected:            
        try:
            #------Check if email number is valid----------------------
            (numMsgs, totalSize) = self.conn_pop3.stat()
            self.debug(200, "Total number of server messages:    ", numMsgs)                
            self.debug(200, "Total size   of server messages:    ", totalSize)
            if  number>numMsgs:
                self.debug(200, "\nSorry - there aren't that many messages in your inbox\n")
                return False
            else:
                (server_msg, body, octets) = self.conn_pop3.retr(number)
                self.debug(200, "Server Message:    "   , server_msg)
                self.debug(200, "Number of Octets:    " , octets)
                self.debug(200, "Message body:")
                for line in body:
                    print line
                #end for
                return True
            #endif
        finally:
            self.__disconnect__()      
    #endif 

这里还有POP3连接,至少我是如何使用字符串比较来实现tricky的it...sort的,但它适用于我的应用程序:

代码语言:javascript
复制
def __connect_pop3__(self):
    """\brief Method for connecting to POP3 server                        
       \return True   If connection to POP3 succeeds or if POP3 is already connected
       \return False  If connection to POP3 fails
    """
    #------Check that POP3 is not already connected-----------------------
    if not self.pop3_connected:
        #------Connect POP3-----------------------------------------------
        self.debug(100, 'Connecting POP3 with: ', self.host_name, self.user_name, self.pass_name)
        self.conn_pop3 = POP3(self.host_name)            
        res1 = self.conn_pop3.user(self.user_name)
        string1 = str(res1)      
        self.debug(100, 'User identification result:', string1) 
        res2 = self.conn_pop3.pass_(self.pass_name)        
        string2 = str(res2)                
        self.debug(100, 'Pass identification result:', string2)                        
        #------Check if connection resulted in success--------------------
        #------Server on DavMail returns 'User successfully logged on'----
        if  string2.find('User successfully logged on')<>-1 or string1.find('User successfully logged on')<>-1 :
            self.pop3_connected = True            
            return True
        else:
            return False
        #endif         
    else:       
        self.debug(255, 'POP3 already connected')
        return True
    #endif 
票数 10
EN

Stack Overflow用户

发布于 2011-12-29 22:48:45

使用POP3示例from the docs

代码语言:javascript
复制
import getpass, poplib
user = 'my_user_name' 
Mailbox = poplib.POP3_SSL('pop.googlemail.com', '995') 
Mailbox.user(user) 
Mailbox.pass_('my_password') 
numMessages = len(Mailbox.list()[1])
for i in range(numMessages):
    for msg in Mailbox.retr(i+1)[1]:
        print msg
Mailbox.quit()
票数 19
EN

Stack Overflow用户

发布于 2015-07-06 16:11:07

如果你想使用IMAP4。使用outlook python库,请在此处下载:https://github.com/awangga/outlook

要从收件箱中检索未读电子邮件,请执行以下操作:

代码语言:javascript
复制
import outlook
mail = outlook.Outlook()
mail.login('emailaccount@live.com','yourpassword')
mail.inbox()
print mail.unread()
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8669202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档