E-Mail 的收发涉及到 STMP 和 POP3 两个协议。下面的代码演示了 STMP 和
POP3 连接的建立
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
from smtplib import SMTP from poplib import POP3 from time import sleep SMTPSVR = 'stmp.163.com' POP3SVR = 'pop.163.com' FROMMAIL = '[email protected]' TOMAIL = '[email protected]' origHdrs = ['From: %s' % FROMMAIL, 'To: %s' % TOMAIL, 'Subject: Just for test'] origBody = ['Test1','Test2','Test3'] origMsg = '/r/n/r/n'.join(['/r/n'.join(origHdrs), '/r/n'.join(origBody)]) sendSvr = SMTP(SMTPSVR) errs = sendSvr.sendmail(FROMMAIL, TOMAIL, origMsg) sendSvr.quit() assert len(errs) == 0, errs sleep(10) recvSvr = POP3(POP3SVR) recvSvr.user('killua_hzl') recvSvr.pass_('123456') rsp, msg, size = recvSvr.retr(recvSvr.stat()[0]) sep = msg.index('') recvBody = msg[sep + 1] assert origBody == recvBody
|