python 接收邮件示例:pop3与imap

首先是pop3与imap的区别:
[img]http://a0.att.hudong.com/62/48/01300000082825121661480571950.gif[/img]
简单来说主要区别就是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()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值