【问题标题】:Get emails with Python and poplib使用 Python 和 poplib 获取电子邮件
【发布时间】:2016-11-06 15:22:53
【问题描述】:

我想用 Python 登录我的帐户并让 Python 打印我在邮箱中收到的消息。我知道如何连接

import getpass, poplib
user = 'my_user_name' 
Mailbox = poplib.POP3_SSL('pop.googlemail.com', '995') 
Mailbox.user(user) 
Mailbox.pass_('my_password') 

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

【问题讨论】:

    标签: python email poplib


    【解决方案1】:

    使用 POP3 示例from the docs

    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()
    

    【讨论】:

    • 你可能想cite the source
    • 拉尔斯曼,谢谢。我忘记在笔记中记录来源。
    • 谢谢。有些消息很有趣,它们是由奇怪的代码组成的,有些则有意义。但我没有按时间顺序排列它们。我试图到达数组的末尾,但它没有返回我收到的最新消息。
    • 关于“奇怪的代码”:您可能希望将消息解析为 email.message.Message 对象。有关如何执行此操作的示例,请参阅this SO answer。在那里,使用 IMAP 而不是 POP3 来检索消息,但在那之后,我认为所有代码都适用于您的情况。因此,“奇怪的代码”可能会被解码为更易读的格式。
    • 像魅力一样工作!!!我将它用于 Outlook,仅进行了此更改 --> Mailbox = poplib.POP3_SSL('pop3.live.com', '995') 非常感谢!
    【解决方案2】:

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

    如何获取消息总数:

    (numMsgs, totalSize) = self.conn_pop3.stat()
    

    如何获取特定邮件,知道其在邮箱中的编号:

    (server_msg, body, octets) = self.conn_pop3.retr(number)
    

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

    小心,它还会在服务器上将相应的电子邮件设置为“已查看”! 您可能可以撤消该操作,至少使用 IMAP 可以。

    我对 pop3 lib 电子邮件的实现如下:

    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 连接,至少我是如何实现它的……使用字符串比较有点棘手,但它适用于我的应用程序:

    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 
    

    【讨论】:

    • 怎么删除邮件,好像.dele然后.quit不行。
    【解决方案3】:

    如果你想使用 IMAP4。使用 Outlook python 库,在这里下载: https://github.com/awangga/outlook

    从您的收件箱中检索未读电子邮件:

    import outlook
    mail = outlook.Outlook()
    mail.login('emailaccount@live.com','yourpassword')
    mail.inbox()
    print mail.unread()
    

    【讨论】:

      猜你喜欢
      • 2018-05-22
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      • 2022-09-27
      • 2021-06-27
      相关资源
      最近更新 更多