python小例子之6 -- pop3協議收取郵件

        主題:pop3協議收取郵件
        環境: winxp pro + sp2 + python2.5
        備註: 請注意,凡是在源代碼文件中使用了中文字符,請最好保存爲utf-8格式,如果Subject爲中文字符,有可能出現亂碼
        代碼:
python 代碼
 
  1. # pop3.py  
  2.   
  3. import poplib  
  4.   
  5. emailServer = poplib.POP3('your pop3 server name')  
  6. emailServer.user('your mail account')  
  7. emailServer.pass_('your mail password')  
  8. # 設置爲1,可查看向pop3服務器提交了什麼命令  
  9. emailServer.set_debuglevel(1)  
  10.   
  11. # 獲取歡迎消息  
  12. serverWelcome = emailServer.getwelcome()  
  13. print serverWelcome  
  14.   
  15. # 獲取一些統計信息  
  16. emailMsgNum, emailSize = emailServer.stat()  
  17. print 'email number is %d and size is %d'%(emailMsgNum, emailSize)  
  18.   
  19. # 遍歷郵件,並打印出每封郵件的標題  
  20. for i in range(emailMsgNum):  
  21.     for piece in emailServer.retr(i+1)[1]:  
  22.         if piece.startswith('Subject'):  
  23.             print '\t' + piece  
  24.             break  
  25.           
  26. emailServer.quit()  

        測試:保存爲文件,把相應帶刪除線的地方修改爲相適應的值,直接執行即可
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章