2011-12-29 246 views
7

Pythonで自分のアカウントにログインし、私のメールボックスで受け取ったメッセージを印刷するpythonを入手したいと思います。私は接続方法を知っていますPythonとpoplibでメールを受信

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

私のメッセージを表示する方法をPythonに教える方法がわかりません。私はpoplib文書のすべての機能を試しました。数字だけが表示されます。 POP3例from the docs使用

答えて

9

は私の応答です:

(numMsgs, totalSize) = self.conn_pop3.stat() 
:メッセージの合計数を取得する方法

特定のメッセージを取得し、そのメールボックス内の番号を知る方法:

したがって、必要な機能がretrであるため、タプルが返されます。 hereを参照してください。

慎重に、それぞれの電子メールをサーバー上で「SEEN」として設定します。 おそらく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 
15

:あなたがここにあなたのソースコードを掲示しますが、していない

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

([ソースを引用] http://docs.python.org/library/: https://github.com/awangga/outlook

があなたの受信トレイから未読メールを取得するために、 poplib.html#pop3-example)を使用してください。 –

+0

larsmans、ありがとう。私はメモをメモに記録することを忘れていました。 – unutbu

+0

ありがとうございます。メッセージの中には面白いものもあります。しかし、私はそれらを年代順に取得しません。 私は配列の終わりに到達しようとしましたが、受け取った最新のメッセージが返されませんでした。 – user1119429

-1

もしあなたがIMAP4を使いたいなら、見通しのPythonライブラリを使用して、ここからダウンロード:

import outlook 
mail = outlook.Outlook() 
mail.login('[email protected]','yourpassword') 
mail.inbox() 
print mail.unread() 
あなたがしたい場合があります
関連する問題