用户工具

站点工具


Command disabled: resendpwd
python-network-application:pop3

Python POP3 收取邮件

python 为我们提供了 poplib 模块,利用这个模块,我们可以很方便的收取邮件。

# -*- coding=GBK -*-
 
import string
import poplib
import StringIO, rfc822
 
servername = "pop3.126.com"
username = "username here"
passwd = "password here"
 
#连接 登录 服务器
pop = poplib.POP3(servername)
pop.set_debuglevel(1)            #会打印出debug信息
pop.user(username)
pop.pass_(passwd)
 
#列出邮件信息
num,total_size = pop.stat()
 
#取得最新的邮件
hdr,text,octet=pop.retr(num)
 
#对邮件进行操作
text = string.join(text, "\n")
file = StringIO.StringIO(text)
 
message = rfc822.Message(file)
 
for k, v in message.items():
    print k, "=", v
 
print message.fp.read()

这里我们使用 rfc822 模块来处理邮件内容,
python提供了email模块来专门处理MIME格式,
我们在 MIME中进行讨论.

python-network-application/pop3.txt · 最后更改: 2010/06/02 01:18 (外部编辑)