# Python core programming network protocol programming email
Email system components:
MTA message transmission proxy, responsible for mail routing, queue and sending
SMTP Simple Mail Transfer Protocol
1. Connect to the server
2 Login
3. Send a service request
4. Exit
Pop: Post Office Protocol
Rfc918 "Post Office Protocol aims to allow users' workstation to access emails in the mailbox server.
Emails must be sent to the mail server via Simple Mail Transfer Protocol SMTP from the workstation"
Use of POP:
1. Connect to the server
2 Login
3. Send a service request
4. Exit
# Coding: utf8 # python2.7 mailtest. py''' to use SMTP and POP3 protocols to send and receive QQ mail experiment user names and passwords, you must enter ''' from smtplib import smtpfrom smtplib import smtprecipientsrefusedfrom poplib import pop3from time import sleep import sys smtpserver = 'smtp .qq.com 'pop3server = 'pop .qq.com 'emailaddr = '2017 @ QQ.com' username = 'xxx' Password = 'xxx' # combined mail format origheaders = ['from: 847915049@qq.com ',' to: 847915049@qq.com ', 'subject: Test msg '] Origbody = ['nihao', 'yaa', 'sichuanc'] origmsg =' \ r \ n \ r \ n '. join (['\ r \ n '. join (origheaders), '\ r \ n '. join (origbody)]) # Sendmail sendser = SMTP (smtpserver) sendser. set_debuglevel (1) print sendser. EHLO () [0] # server attributes and other sendser. login (username, password) # verify the QQ mailbox try: errs = sendser. sendmail (emailaddr, emailaddr, origmsg) failed t smtprecipientsrefused: Print 'server refused .... 'sys. exit (1) sendser. quit () assert Len (err S) = 0, errs print '\ n \ nsend a mail... OK! 'Sleep (10) # Wait for 10 seconds to print 'Now get the mail ..... \ n \ n' # start to receive the email 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 ....'
Result:
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 ....