dream789 发表于 2018-8-14 13:34:21

python 收发邮件

#encoding=utf-8  
import poplib
  
import email
  

  
if __name__ == '__main__':
  
      M = poplib.POP3('pop.163.com')
  
      M.user('BuGaoSuNi@163.com')
  
      M.pass_('BuGaoSuNi')
  

  
      #打印有多少封信
  
      numMessages = len(M.list())
  
      print 'num of messages', numMessages
  

  

  
      #从最老的邮件开始遍历
  
      for i in range(numMessages):
  
                m = M.retr(i+1)
  
                msg = email.message_from_string('\n'.join(m))
  
                #allHeaders = email.Header.decode_header(msg)
  
                aimHeaderStrs = {'from':'', 'to':'', 'subject':''}
  
                for aimKey in aimHeaderStrs.keys():
  
                        aimHeaderList = email.Header.decode_header(msg)
  
                        for tmpTuple in aimHeaderList:
  
                              if tmpTuple == None:
  
                                        aimHeaderStrs += tmpTuple
  
                              else:
  
                                        aimHeaderStrs += tmpTuple.decode(tmpTuple) #转成unicode
  
                for aimKey in aimHeaderStrs.keys():
  
                        print aimKey,':',aimHeaderStrs.encode('utf-8') #转成utf-8显示
  

  
                for part in msg.walk(): #遍历所有payload
  
                        contenttype = part.get_content_type()
  
                        filename = part.get_filename()
  
                        if filename: #and contenttype=='application/octet-stream':
  
                              #保存附件
  
                              data = part.get_payload(decode=True)
  
                              file("mail%d.attach.%s" % (i+1,filename),'wb').write(data)
  
                        elif contenttype == 'text/plain':
  
                              #保存正文
  
                              data = part.get_payload(decode=True)
  
                              charset = part.get_content_charset('ios-8859-1')
  
                              file('mail%d.txt' % (i+1), 'w').write(data.decode(charset).encode('utf-8'))
页: [1]
查看完整版本: python 收发邮件