`
chembo
  • 浏览: 922954 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

python 接收邮件示例:pop3与imap

阅读更多
首先是pop3与imap的区别:

简单来说主要区别就是imap可以不用把所有的邮件全部下载,就通过客户端直接对服务器上的邮件进行操作。IMAP它只下载邮件的主题,并不是把所有的邮件内容都下载下来.

=============================pop3=================================
import poplib  
  
emailServer = poplib.POP3('192.168.88.7')  
emailServer.user('qa01@corp.globalmarket.com')  
emailServer.pass_('123456')  

# 获取一些统计信息  
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() 



=============================imap=================================
import imaplib, string, email
M = imaplib.IMAP4_SSL("imap.gmail.com")
print M
try:
    try:
        M.login('chemboking@gmail.com','12345678')
    except Exception,e:
        print 'login error: %s' % e
        M.close()
    M.select()
    result, message = M.select()
    typ, data = M.search(None, 'ALL')
    for num in string.split(data[0]):
        try:
            typ, data = M.fetch(num, '(RFC822)')
            msg = email.message_from_string(data[0][1])
            print msg["From"]
            print msg["Subject"]
            print msg["Date"]
            print "_______________________________"
        except Exception,e:
            print 'got msg error: %s' % e            
    M.logout()
    M.close()
except Exception, e:
    print 'imap error: %s' % e
    M.close()

分享到:
评论

相关推荐

    Python编程入门经典

    16.3.4 安全的POP3和IMAP 294 16.3.5 Webmail应用程序不是 E-mail应用程序 294 16.4 套接字编程 294 16.4.1 套接字简介 295 16.4.2 绑定到外部主机名 297 16.4.3 镜像服务器 298 16.4.4 镜像客户端 299 16.4.5 套接...

    Python核心编程第二版

     1.7 比较Python(Python与其他语言的比较)   1.8 其他实现   1.9 练习   第2章 快速入门   2.1 程序输出,print语句及“Hello World!”   2.2 程序输入和raw_input()内建函数   2.3 注释  ...

    Python核心编程第二版(ok)

     1.7 比较Python(Python与其他语言的比较)   1.8 其他实现   1.9 练习   第2章 快速入门   2.1 程序输出,1print语句及“HellocWorld!”   2.2 程序输入和raw_input()内建函数   2.3 注释  ...

    nginx(1-9-8).syno.tar官方镜像

    Nginx是用于HTTP,HTTPS,SMTP,POP3和IMAP协议的开源反向代理服务器,以及负载平衡器,HTTP缓存和Web服务器(原始服务器)。Nginx项目一开始就非常关注高并发,高性能和低内存使用。它获得了两节式BSD许可,并在...

    RED HAT LINUX 6大全

    7.5.3 为POP3或者IMAP邮件获取配置 fetchmail 133 7.6 小结 133 第8章 FTP 135 8.1 获取和安装FTP服务程序 135 8.2 FTP服务器的工作方式 137 8.3 配置FTP服务器 138 8.3.1 控制访问权—/etc/ftpaccess 文件 138 ...

Global site tag (gtag.js) - Google Analytics