龍子 发表于 2017-5-5 10:41:16

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

        主题:pop3协议收取邮件
        环境: winxp pro + sp2 + python2.5
        备注: 请注意,凡是在源代码文件中使用了中文字符,请最好保存为utf-8格式,如果Subject为中文字符,有可能出现乱码
        代码:
python 代码
 

[*]# 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):  
[*]        if piece.startswith('Subject'):  
[*]            print '\t' + piece  
[*]            break  
[*]          
[*]emailServer.quit()  


        测试:保存为文件,把相应带删除线的地方修改为相适应的值,直接执行即可
页: [1]
查看完整版本: python小例子之6 -- pop3协议收取邮件