POP3メールクライアントプログラム



Pop3 Mail Client Program



1はじめに:

メールを送信するSMTPプログラムを作成する前に、メールを受け入れて表示することはできません。今回研究したPOP3プロトコルは、この問題を解決するためのものです。これは主に、サーバーへのログイン、受信トレイのメールの確認、およびいくつかの簡単な操作の実行に使用されます。

2.環境のデバッグ:

SMTPプロトコルと同じです。



3.プロセス:

1.譲歩フェーズでは、ユーザーエージェントがユーザー名とパスワードを送信します

2.トランザクション処理段階:ユーザーエージェントは返信メッセージを取得し、メッセージマークを削除したり、メッセージ削除マークをキャンセルしたり、メールを取得したりすることもできます。
統計



3.更新段階:この時点でPOP3セッションを終了し、メールサーバーはマークされた削除済みメッセージを削除します。

4.単純な処理コード(他の機能を追加できます):

主にいくつかの関連する機能の使用を参照してください:

import poplib from email.parser import Parser from email.header import decode_header from email.utils import parseaddr def login_in():#Login phase email=input('Email:') password=input('Password:') pop_server=input('Pop3 server:') global server server=poplib.POP3(pop_server) server.set_debuglevel(1) print(server.getwelcome().decode('utf-8')) server.user(email) server.pass_(password) def handle_command(user_cmd,email_title):#Processing commands if user_cmd=='1': #list the main information print('Total message number: %s Total size: %s' % server.stat()) elif user_cmd=='2': resp,lines,octets=server.retr(email_title) msg_content=b' '.join(lines).decode('utf-8') msg=Parser().parsestr(msg_content) print_infor(msg) else : print('error command! ') def print_infor(msg,indent=0):#Output information if indent==0: for header in ['From','To','Subject']: value=msg.get(header,'') if value: if header=='Subject': value=decode_str(value) else : hdr,addr=parseaddr(value) name=decode_str(hdr) value=u'%s '%(name,addr) print('%s%s: %s'%(' '*indent,header,value)) if msg.is_multipart(): parts=msg.get_payload() for n,part in enumerate(parts): print('%spart %s'%(' '*indent,n)) print('%s---------------'%(' '*indent)) print_infor(part,indent+1) else: content_type=msg.get_content_type() if content_type=='text/plain' or content_type=='text/html': content=msg.get_payload(decode=True) charset=guess_charset(msg) if charset: content=content.decode(charset) print('%sText: %s'%(' '*indent,content+'...')) else: print('%sAttachment :%s'%(' '*indent,content_type)) def decode_str(s):#decoding value,charset=decode_header(s)[0] if charset: value=value.decode(charset) return value def guess_charset(msg): charset=msg.get_charset() if charset is None: content_type=msg.get('Content-Type','').lower() pos=content_type.find('charset=') if pos>=0: charset=content_type[pos+8:].strip() return charset def start():#Start login_in() resp,mails,octets=server.list() print(mails) index=len(mails) print(' --------------------------------------------') print('input '0' you can quit input '1' you can get the whole information. input '2' you can see a specific email ') print('-------------------------------------------- ') while(1): command=input('please input your commands: ') if command=='0': break elif command=='2': email_num=input('please input the email number you want to see:') handle_command(command,email_num) else: handle_command(command,0) print(' ') server.quit() print('you quit the pop3 successfully!') if __name__ == '__main__': start()

5.結果:

画像



添付ファイル:コマンドコード(Baidu百科事典からの抜粋):

画像

注:著者がインターネット上で情報を収集した後、上記のすべての操作は成功し、パーソナルコンピューターでの実験は成功します。読者が実験に失敗した場合、それはいくつかの未知の要因によって引き起こされている可能性があります。あなたは作者に連絡することができます。書かれたチュートリアルは過失のために間違っているかもしれません、作者に連絡してください。