Python-network-programming-python-pop3

提供:Dev Guides
移動先:案内検索

Python-POP3

pop3プロトコルは、電子メールサーバーからメッセージをダウンロードするための電子メールプロトコルです。 これらのメッセージはローカルマシンに保存できます。

キーポイント

  • POPは、アプリケーション層のインターネット標準プロトコルです。
  • POPはメッセージへのオフラインアクセスをサポートしているため、インターネットの使用時間が短縮されます。
  • POPは検索機能を許可しません。
  • メッセージにアクセスするには、それらをダウンロードする必要があります。
  • サーバー上に作成できるメールボックスは1つだけです。
  • メール以外のデータへのアクセスには適していません。
  • 通常、POPコマンドは3文字または4文字のコードに短縮されます。 Eg. STAT。

POPコマンド

次の表に、POPコマンドの一部を示します。

S.N. Command Description
1 LOGINThis command opens the connection.
2 STATIt is used to display number of messages currently in the mailbox.
3 LISTIt is used to get the summary of messages where each message summary is shown.
4 RETRThis command helps to select a mailbox to access the messages.
5 DELEIt is used to delete a message.
6 RSETIt is used to reset the session to its initial state.
7 QUITIt is used to log off the session.

Pyhtonの poplib モジュールは、この要件を達成するために使用されるpop()およびpop3_SSL()という名前のクラスを提供します。 引数としてホスト名とポート番号を指定します。 以下の例では、Gmailサーバーに接続し、ログイン認証情報を提供した後にメッセージを取得します。

import  poplib

user = 'username'
# Connect to the mail box
Mailbox = poplib.POP3_SSL('pop.googlemail.com', '995')
Mailbox.user(user)
Mailbox.pass_('password')
NumofMessages = len(Mailbox.list()[1])
for i in range(NumofMessages):
    for msg in Mailbox.retr(i+1)[1]:
        print msg
Mailbox.quit()

上記のプログラムを実行すると、メッセージが取得されます。