[Python]使用smtp和pop簡單收發郵件

來源:互聯網
上載者:User

#Python 核心編程網路通訊協定編程 Email

email系統組件:
MTA 訊息傳輸代理,負責郵件的路由,隊列和發送

SMTP 簡易郵件傳輸通訊協定
1 串連到伺服器
2 登陸
3 發出服務要求
4 退出

POP:郵局協議
RFC918 "郵局協議的目的是讓使用者的工作站可以訪問到郵箱伺服器裡的郵件。
郵件要能從工作站通過簡易郵件傳輸通訊協定SMTP發送到郵件伺服器"

POP的使用:
1 串連到伺服器
2 登陸
3 發出服務要求

4 退出

#coding:utf8#python2.7 mailtest.py'''使用smtp和pop3 協議收發qq郵箱實驗 使用者名稱和密碼需要自己填寫'''from smtplib import SMTPfrom smtplib import SMTPRecipientsRefusedfrom poplib import POP3from 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)])#發送郵件部分sendSer = SMTP(smtpserver)sendSer.set_debuglevel(1)print sendSer.ehlo()[0] #伺服器屬性等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 ....'

結果:

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.comPIPELININGSIZE 52428800AUTH LOGIN PLAINAUTH=LOGINMAILCOMPRESS8BITMIME250send: 'AUTH PLAIN ADg0NzkxNTA0OQA0OTMzODQ4MTIzNA==\r\n'reply: '235 Authentication successful\r\n'reply: retcode (235); Msg: Authentication successfulsend: 'mail FROM:<847915049@qq.com> size=88\r\n'reply: '250 Ok\r\n'reply: retcode (250); Msg: Oksend: 'rcpt TO:<847915049@qq.com>\r\n'reply: '250 Ok\r\n'reply: retcode (250); Msg: Oksend: '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 asdata: (250, 'Ok: queued as')send: 'quit\r\n'reply: '221 Bye\r\n'reply: retcode (221); Msg: Byesend a mail ....OK!Now get the mail .....Date: Mon, 22 Apr 2013 16:22:01 +0800X-QQ-mid: esmtp26t1366618921t440t12695Received: from [169.254.114.107] (unknown [120.210.224.173])by esmtp4.qq.com (ESMTP) with SMTP id 0for <847915049@qq.com>; Mon, 22 Apr 2013 16:22:01 +0800 (CST)X-QQ-SSF: B101000000000050321003000000000From: 847915049@qq.comTo: 847915049@qq.comSubject: test msgnihao yaansichuansuccessful get ....
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.