import poplib
import mime
import cStringIO
import traceback
import mimeheader

p=poplib.POP3("prm-cl1")
assert(p.user("sergey")[:3]=="+OK")
assert(p.pass_("yuGVf54f-IHu!h$j%v^m")[:3]=="+OK")
count=p.stat()[0]
#print count
for a in xrange(1,count+1):
  try:
#    t=p.top(a,0)
#    assert(t[0]=="+OK")
#    h=mime.MIMEPart('\n'.join(t[1]))
#    try:
#      print str(h.get_header("Content-Transfer-Encoding")).strip()
#    except:
#      print "encoding unknown"
#    x=h.get_header("Subject").value
#    print `x`
#    fr=mimeheader.decode(x.rstrip())
#    print fr
#    print base64.decodestring(str(h.get_header("Subject")).strip())

  # save and delete attaches, reply with number of attaches saved and their
  # names

#    r=p.retr(a)
#    assert(r[0]=="+OK")
#    m=mime.MIMEPart('\n'.join(r[1]))
    #dele
  except:
    print "message %d failed"%a
    traceback.print_exc()

p.quit()

