Python POP 3受信メール

942 ワード

pythonはpoplibモジュールを提供してくれて、このモジュールを利用して、私たちは簡単にメールを受け取ることができます.
# -*- coding=GBK -*-   import string
import poplib
import StringIO, rfc822   servername = "pop3.126.com"
username = "username here"
passwd = "password here"   #         
pop = poplib.POP3(servername)
pop.set_debuglevel(1)            #    debug  
pop.user(username)
pop.pass_(passwd)   #      
num,total_size = pop.stat()   #       
hdr,text,octet=pop.retr(num)   #       
text = string.join(text, "
") file = StringIO.StringIO(text)   message = rfc822.Message(file)   for k, v in message.items(): print k, "=", v   print message.fp.read()

ここではrfc 822モジュールを用いてメールの内容を処理する、pythonはMIMEフォーマットを専門に処理するemailモジュールを提供し、MIMEで議論する.