環境: winxp pro + sp2 + python2.5
備註: 請注意,凡是在源代碼文件中使用了中文字符,請最好保存爲utf-8格式,如果Subject爲中文字符,有可能出現亂碼
代碼:
- # pop3.py
- import poplib
- emailServer = poplib.POP3('
your pop3 server name') - emailServer.user('
your mail account') - emailServer.pass_('
your mail password') - # 設置爲1,可查看向pop3服務器提交了什麼命令
- emailServer.set_debuglevel(1)
- # 獲取歡迎消息
- serverWelcome = emailServer.getwelcome()
- print serverWelcome
- # 獲取一些統計信息
- emailMsgNum, emailSize = emailServer.stat()
- print 'email number is %d and size is %d'%(emailMsgNum, emailSize)
- # 遍歷郵件,並打印出每封郵件的標題
- for i in range(emailMsgNum):
- for piece in emailServer.retr(i+1)[1]:
- if piece.startswith('Subject'):
- print '\t' + piece
- break
- emailServer.quit()
測試:保存爲文件,把相應帶