脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python使用smtp和pop简单收发邮件完整实例

Python使用smtp和pop简单收发邮件完整实例

2021-01-03 00:25orangleliu Python

这篇文章主要介绍了Python使用smtp和pop简单收发邮件完整实例,简单介绍了smtp和pop,然后分享了相关实例代码,具有一定借鉴价值,需要的朋友可以参考下

SMTP

SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。

Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件。

pop

收取邮件就是编写一个MUA作为客户端,从MDA把邮件获取到用户的电脑或者手机上。收取邮件最常用的协议是POP协议,目前版本号是3,俗称POP3。

Python内置一个poplib模块,实现了POP3协议,可以直接用来收邮件。

注意到POP3协议收取的不是一个已经可以阅读的邮件本身,而是邮件的原始文本,这和SMTP协议很像,SMTP发送的也是经过编码后的一大段文本。

要把POP3收取的文本变成可以阅读的邮件,还需要用email模块提供的各种类来解析原始文本,变成可阅读的邮件对象。

所以,收取邮件分两步:

第一步:用poplib把邮件的原始文本下载到本地;

第二部:用email解析原始文本,还原为邮件对象。

email系统组件:

MTA消息传输代理,负责邮件的路由,队列和发送

SMTP简单邮件传输协议

1连接到服务器

2登陆

3发出服务请求

4退出

POP:邮局协议

RFC918"邮局协议的目的是让用户的工作站可以访问到邮箱服务器里的邮件。

邮件要能从工作站通过简单邮件传输协议SMTP发送到邮件服务器"

POP的使用:

1连接到服务器

2登陆

3发出服务请求

4退出

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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 ....'

结果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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 ....

总结

以上就是本文关于Python使用smtp和pop简单收发邮件完整实例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/orangleliu/article/details/8835137

延伸 · 阅读

精彩推荐