使用Python和poplib获取电子邮件

2024-04-26 07:04:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用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文档中的所有功能。它们只显示数字。


Tags: nameimportcom消息sslmy帐户pop
3条回答

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

如果你想使用IMAP4。使用outlook python库,下载位置: https://github.com/awangga/outlook

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

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

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

如何获取邮件总数:

(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 

相关问题 更多 >