[Python]使用smtp和pop简单收发邮件
#Python 核心编程网络协议编程 Email
email系统组件:
MTA 消息传输代理,负责邮件的路由,队列和发送
SMTP 简单邮件传输协议
1 连接到服务器
2 登陆
3 发出服务请求
4 退出
POP:邮局协议
RFC918 "邮局协议的目的是让用户的工作站可以访问到邮箱服务器里的邮件。
邮件要能从工作站通过简单邮件传输协议SMTP发送到邮件服务器"
POP的使用:
1 连接到服务器
2 登陆
3 发出服务请求
4 退出
[python]
#coding:utf8
#python2.7 mailtest.py
'''''
使用smtp和pop3 协议收发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)])
#发送邮件部分
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 ....'
#coding:utf8
#python2.7 mailtest.py
'''
使用smtp和pop3 协议收发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)])
#发送邮件部分
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 ....'
结果:
[plain]
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 ....
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 successf
补充:Web开发 , Python ,