Python使用smtp和pop簡(jiǎn)單收發(fā)郵件完整實(shí)例
SMTP
SMTP是發(fā)送郵件的協(xié)議,Python內(nèi)置對(duì)SMTP的支持,可以發(fā)送純文本郵件、HTML郵件以及帶附件的郵件。
Python對(duì)SMTP支持有smtplib和email兩個(gè)模塊,email負(fù)責(zé)構(gòu)造郵件,smtplib負(fù)責(zé)發(fā)送郵件。
pop
收取郵件就是編寫(xiě)一個(gè)MUA作為客戶端,從MDA把郵件獲取到用戶的電腦或者手機(jī)上。收取郵件最常用的協(xié)議是POP協(xié)議,目前版本號(hào)是3,俗稱POP3。
Python內(nèi)置一個(gè)poplib模塊,實(shí)現(xiàn)了POP3協(xié)議,可以直接用來(lái)收郵件。
注意到POP3協(xié)議收取的不是一個(gè)已經(jīng)可以閱讀的郵件本身,而是郵件的原始文本,這和SMTP協(xié)議很像,SMTP發(fā)送的也是經(jīng)過(guò)編碼后的一大段文本。
要把POP3收取的文本變成可以閱讀的郵件,還需要用email模塊提供的各種類來(lái)解析原始文本,變成可閱讀的郵件對(duì)象。
所以,收取郵件分兩步:
第一步:用poplib把郵件的原始文本下載到本地;
第二部:用email解析原始文本,還原為郵件對(duì)象。
email系統(tǒng)組件:
MTA消息傳輸代理,負(fù)責(zé)郵件的路由,隊(duì)列和發(fā)送
SMTP簡(jiǎn)單郵件傳輸協(xié)議
1連接到服務(wù)器
2登陸
3發(fā)出服務(wù)請(qǐng)求
4退出
POP:郵局協(xié)議
RFC918"郵局協(xié)議的目的是讓用戶的工作站可以訪問(wèn)到郵箱服務(wù)器里的郵件。
郵件要能從工作站通過(guò)簡(jiǎn)單郵件傳輸協(xié)議SMTP發(fā)送到郵件服務(wù)器"
POP的使用:
1連接到服務(wù)器
2登陸
3發(fā)出服務(wù)請(qǐng)求
4退出
#coding:utf8 #python2.7 mailtest.py ''''' 使用smtp和pop3 協(xié)議收發(fā)qq郵箱實(shí)驗(yàn) 用戶名和密碼需要自己填寫(xiě) ''' 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郵箱需要驗(yàn)證 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' #開(kāi)始接收郵件 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簡(jiǎn)單收發(fā)郵件完整實(shí)例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
python常用web框架簡(jiǎn)單性能測(cè)試結(jié)果分享(包含django、flask、bottle、tornado)
這篇文章主要介紹了python常用web框架簡(jiǎn)單性能測(cè)試結(jié)果分享(包含django、flask、bottle、tornado),需要的朋友可以參考下2014-08-08Python多線程多進(jìn)程實(shí)例對(duì)比解析
這篇文章主要介紹了Python多線程多進(jìn)程實(shí)例對(duì)比解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03用python爬取中國(guó)大學(xué)排名網(wǎng)站排名信息
大家好,本篇文章主要講的是用python爬取中國(guó)大學(xué)排名網(wǎng)站排名信息,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01Python編程根據(jù)字典列表相同鍵的值進(jìn)行合并
這篇文章主要介紹了來(lái)學(xué)習(xí)Python字典列表根據(jù)相同鍵的值進(jìn)行合并的操作方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10python3實(shí)現(xiàn)磁盤(pán)空間監(jiān)控
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)磁盤(pán)空間監(jiān)控,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06python獲取當(dāng)前運(yùn)行函數(shù)名稱的方法實(shí)例代碼
這篇文章主要介紹了python獲取當(dāng)前運(yùn)行函數(shù)名稱的方法實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04