Python使用smtp和pop簡單收發(fā)郵件完整實例
SMTP
SMTP是發(fā)送郵件的協(xié)議,Python內(nèi)置對SMTP的支持,可以發(fā)送純文本郵件、HTML郵件以及帶附件的郵件。
Python對SMTP支持有smtplib和email兩個模塊,email負責構(gòu)造郵件,smtplib負責發(fā)送郵件。
pop
收取郵件就是編寫一個MUA作為客戶端,從MDA把郵件獲取到用戶的電腦或者手機上。收取郵件最常用的協(xié)議是POP協(xié)議,目前版本號是3,俗稱POP3。
Python內(nèi)置一個poplib模塊,實現(xiàn)了POP3協(xié)議,可以直接用來收郵件。
注意到POP3協(xié)議收取的不是一個已經(jīng)可以閱讀的郵件本身,而是郵件的原始文本,這和SMTP協(xié)議很像,SMTP發(fā)送的也是經(jīng)過編碼后的一大段文本。
要把POP3收取的文本變成可以閱讀的郵件,還需要用email模塊提供的各種類來解析原始文本,變成可閱讀的郵件對象。
所以,收取郵件分兩步:
第一步:用poplib把郵件的原始文本下載到本地;
第二部:用email解析原始文本,還原為郵件對象。
email系統(tǒng)組件:
MTA消息傳輸代理,負責郵件的路由,隊列和發(fā)送
SMTP簡單郵件傳輸協(xié)議
1連接到服務(wù)器
2登陸
3發(fā)出服務(wù)請求
4退出
POP:郵局協(xié)議
RFC918"郵局協(xié)議的目的是讓用戶的工作站可以訪問到郵箱服務(wù)器里的郵件。
郵件要能從工作站通過簡單郵件傳輸協(xié)議SMTP發(fā)送到郵件服務(wù)器"
POP的使用:
1連接到服務(wù)器
2登陸
3發(fā)出服務(wù)請求
4退出
#coding:utf8 #python2.7 mailtest.py ''''' 使用smtp和pop3 協(xié)議收發(fā)qq郵箱實驗 用戶名和密碼需要自己填寫 ''' from smtplib import SMTP from smtplib import SMTPRecipientsRefused from poplib import POP3 from time import sleep import sys smtpserver = 'smtp.qq.com' pop3server = 'pop.qq.com' emailaddr = '847915049@qq.com' username = 'XXX' password = 'XXX' #組合郵件格式 origHeaders = ['From: 847915049@qq.com', 'To: 847915049@qq.com', 'Subject: test msg'] origBody = ['nihao ','yaan','sichuan'] origMsg = '\r\n\r\n'.join(['\r\n'.join(origHeaders),'\r\n'.join(origBody)]) #發(fā)送郵件部分 sendSer = SMTP(smtpserver) sendSer.set_debuglevel(1) print sendSer.ehlo()[0] #服務(wù)器屬性等 sendSer.login(username,password) #qq郵箱需要驗證 try: errs = sendSer.sendmail(emailaddr,emailaddr,origMsg) except SMTPRecipientsRefused: print 'server refused....' sys.exit(1) sendSer.quit() assert len(errs) == 0,errs print '\n\n\nsend a mail ....OK!' sleep(10) #等待10秒 print 'Now get the mail .....\n\n\n' #開始接收郵件 revcSer = POP3(pop3server) revcSer.user(username) revcSer.pass_(password) rsp,msg,siz = revcSer.retr(revcSer.stat()[0]) sep = msg.index('') if msg: for i in msg: print i revcBody = msg[sep+1:] assert origBody == revcBody print 'successful get ....'
結(jié)果:
send: 'ehlo [169.254.114.107]\r\n' reply: '250-smtp.qq.com\r\n' reply: '250-PIPELINING\r\n' reply: '250-SIZE 52428800\r\n' reply: '250-AUTH LOGIN PLAIN\r\n' reply: '250-AUTH=LOGIN\r\n' reply: '250-MAILCOMPRESS\r\n' reply: '250 8BITMIME\r\n' reply: retcode (250); Msg: smtp.qq.com PIPELINING SIZE 52428800 AUTH LOGIN PLAIN AUTH=LOGIN MAILCOMPRESS 8BITMIME 250 send: 'AUTH PLAIN ADg0NzkxNTA0OQA0OTMzODQ4MTIzNA==\r\n' reply: '235 Authentication successful\r\n' reply: retcode (235); Msg: Authentication successful send: 'mail FROM:<847915049@qq.com> size=88\r\n' reply: '250 Ok\r\n' reply: retcode (250); Msg: Ok send: 'rcpt TO:<847915049@qq.com>\r\n' reply: '250 Ok\r\n' reply: retcode (250); Msg: Ok send: 'data\r\n' reply: '354 End data with <CR><LF>.<CR><LF>\r\n' reply: retcode (354); Msg: End data with <CR><LF>.<CR><LF> data: (354, 'End data with <CR><LF>.<CR><LF>') send: 'From: 847915049@qq.com\r\nTo: 847915049@qq.com\r\nSubject: test msg\r\n\r\nnihao \r\nyaan\r\nsichuan\r\n.\r\n' reply: '250 Ok: queued as \r\n' reply: retcode (250); Msg: Ok: queued as data: (250, 'Ok: queued as') send: 'quit\r\n' reply: '221 Bye\r\n' reply: retcode (221); Msg: Bye send a mail ....OK! Now get the mail ..... Date: Mon, 22 Apr 2013 16:22:01 +0800 X-QQ-mid: esmtp26t1366618921t440t12695 Received: from [169.254.114.107] (unknown [120.210.224.173]) by esmtp4.qq.com (ESMTP) with SMTP id 0 for <847915049@qq.com>; Mon, 22 Apr 2013 16:22:01 +0800 (CST) X-QQ-SSF: B101000000000050321003000000000 From: 847915049@qq.com To: 847915049@qq.com Subject: test msg nihao yaan sichuan successful get ....
總結(jié)
以上就是本文關(guān)于Python使用smtp和pop簡單收發(fā)郵件完整實例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
python常用web框架簡單性能測試結(jié)果分享(包含django、flask、bottle、tornado)
這篇文章主要介紹了python常用web框架簡單性能測試結(jié)果分享(包含django、flask、bottle、tornado),需要的朋友可以參考下2014-08-08