# 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
[Python]
# Coding: utf8
# Python2.7 mailtest. py
'''''
Use smtp and pop3 protocols to send and receive QQ mail experiments
Enter your username and password.
'''
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 = '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)])
# Sending emails
SendSer = SMTP (smtpserver)
SendSer. set_debuglevel (1)
Print sendSer. ehlo () [0] # server attributes, etc.
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 (errs) = 0, errs
Print '\ n \ nsend a mail... OK! '
Sleep (10) # Wait 10 seconds
Print 'Now get the mail... \ n'
# Start receiving emails
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 ....'
# Coding: utf8
# Python2.7 mailtest. py
'''
Use smtp and pop3 protocols to send and receive QQ mail experiments
Enter your username and password.
'''
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 = '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)])
# Sending emails
SendSer = SMTP (smtpserver)
SendSer. set_debuglevel (1)
Print sendSer. ehlo () [0] # server attributes, etc.
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 (errs) = 0, errs
Print '\ n \ nsend a mail... OK! '
Sleep (10) # Wait 10 seconds
Print 'Now get the mail... \ n'
# Start receiving emails
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:
[Plain]
Send: 'ehlo [169.254.114.107] \ r \ N'
Reply: '2017 -smtp.qq.com \ r \ N'
Reply: '2017-PIPELINING \ r \ N'
Reply: '2017-SIZE 250 \ r \ N'
Reply: '2017-auth login plain \ r \ N'
Reply: '2017-AUTH = LOGIN \ r \ N'
Reply: '2017-MAILCOMPRESS \ r \ N'
Reply: '2017 8BITMIME \ r \ N'
Reply: retcode (250); Msg: smtp.qq.com
PIPELINING
SIZE 52428800
AUTH LOGIN PLAIN
AUTH = LOGIN
MAILCOMPRESS
8 BITMIME
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: '2017 OK \ r \ N'
Reply: retcode (250); Msg: OK
Send: 'fig TO: <847915049@qq.com> \ r \ N'
Reply: '2017 OK \ r \ N'
Reply: retcode (250); Msg: OK
Send: 'Data \ r \ N'
Reply: '1970 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: '100 OK: queued as \ r \ N'
Reply: retcode (250); Msg: OK: queued
Data: (250, 'OK: queued ')
Send: 'Quit \ r \ N'
Reply: '2017 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: esmtp26t1366618921t425t12695
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: b10000000000050321003000000000
From: 847915049@qq.com
To: 847915049@qq.com
Subject: test msg
Nihao
Yaan
Sichuan
Successful get ....
Send: 'ehlo [169.254.114.107] \ r \ N'
Reply: '2017 -smtp.qq.com \ r \ N'
Reply: '2017-PIPELINING \ r \ N'
Reply: '2017-SIZE 250 \ r \ N'
Reply: '2017-auth login plain \ r \ N'
Reply: '2017-AUTH = LOGIN \ r \ N'
Reply: '2017-MAILCOMPRESS \ r \ N'
Reply: '2017 8BITMIME \ r \ N'
Reply: retcode (250); Msg: smtp.qq.com
PIPELINING
SIZE 52428800
AUTH LOGIN PLAIN
AUTH = LOGIN
MAILCOMPRESS
8 BITMIME
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: '2017 OK \ r \ N'
Reply: retcode (250); Msg: OK
Send: 'fig TO: <847915049@qq.com> \ r \ N'
Reply: '2017 OK \ r \ N'
Reply: retcode (250); Msg: OK
Send: 'Data \ r \ N'
Reply: '1970 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: '100 OK: queued as \ r \ N'
Reply: retcode (250); Msg: OK: queued
Data: (250, 'OK: queued ')
Send: 'Quit \ r \ N'
Reply: '2017 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: esmtp26t1366618921t425t12695
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: b10000000000050321003000000000
From: 847915049@qq.com
To: 847915049@qq.com
Subject: test msg
Nihao
Yaan
Sichuan
Successful get ....