python Socket编程(三)----SMTP POP3

开门上例子:

'''
Created on 2012-3-9

@author: Administrator
'''
#!/usr/bin/env python

from smtplib import SMTP
from poplib import POP3
from time import sleep

SMTPSVR='smtp.163.com'
POP3SVR='pop3.126.com'

origHdrs=['From:[email protected]',
          'To:[email protected]',
          'Subject:testMsg']
origBody=['xxx','yyy','zzz']
origMsg='\r\n\r\n'.join(['\r\n'.join(origHdrs),'\r\n'.join(origBody)])
print origMsg
sendSvr=SMTP(SMTPSVR)
sendSvr.login('XXXX', 'AAAA')
errs=sendSvr.sendmail('[email protected]', ('[email protected]',), origMsg)
sendSvr.quit()
assert len(errs) ==0,errs
print 'smtp over!'

recvSvr=POP3(POP3SVR)
recvSvr.user('YYYY')
recvSvr.pass_('BBBB')
rsp,meg,siz=recvSvr.retr(recvSvr.stat()[0])
sep=meg.index('')
recvBody=meg[sep+1:]
print recvBody


例子很简单smtp注意origMsg拼凑的时候的规则,pop3注意
retr选择最近的一封,剩下的就是把xxxx、yyyy、aaaa、bbbb换成自己的邮件用户名和密码即可。




你可能感兴趣的:(socket编程)