---
title: "python小例子之6 -- pop3协议收取邮件"
author: "weistar"
date: "2014-03-19T14:39:51Z"
tags: []
categories: ["编程语言"]
slug: "python-6-pop3"
draft: true
lastmod: "2014-03-19T14:39:50Z"
id: 2032936
url: "https://weistar.iteye.com/blog/2032936"
---

文章源自：http://bluecrystal.iteye.com/blog/118089

### python小例子之6 -- pop3协议收取邮件

        主题：pop3协议收取邮件  
        环境： 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() 
```