9

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ドキュメントのすべての関数を試しました。数字のみを表示します。

4

3 に答える 3

19

ドキュメントのPOP3の例を使用します。

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()
于 2011-12-29T14:48:45.427 に答える
10

あなたはあなたのソースコードを投稿していませんが、ここに私の応答があります:

メッセージの総数を取得する方法:

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

メールボックス内の番号を知っている特定のメッセージを取得する方法:

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

したがって、必要になる可能性のある関数はretrであり、タプルを返します。ここを参照 してください。

また、サーバー上でそれぞれの電子メールをSEENとして設定するように注意してください。少なくともIMAPを使用すれば、おそらくそれを元に戻すことができます。

そして、pop3lib電子メールの私の実装は次のようになりました。

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 
于 2011-12-29T15:05:02.730 に答える