-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpop3.py
44 lines (40 loc) · 1.43 KB
/
pop3.py
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
#-*- coding: utf8 -*-
import poplib
# 输入邮件地址, 口令和POP3服务器地址:
# email = raw_input('Email:')#data.intern@yrbrands.com
# password = raw_input('Password:')#Password2015
# pop3_server = raw_input('POP3 server:') #outlook.office365.com,995
email = 'data.intern@yrbrands.com'
password = 'Password2015'
pop3_server = 'outlook.office365.com'
# 连接到POP3服务器:
server = poplib.POP3_SSL(pop3_server)
# 可以打开或关闭调试信息:
server.set_debuglevel(1)
# 可选:打印POP3服务器的欢迎文字:
print(server.getwelcome())
# 身份认证:
server.user(email)
server.pass_(password)
# stat()返回邮件数量和占用空间:
print('Messages: %s. Size: %s' % server.stat())
# list()返回所有邮件的编号:
resp, mails, octets = server.list()
# 可以查看返回的列表类似['1 82923', '2 2184', ...]
print(mails)
# 获取最新一封邮件, 注意索引号从1开始:
index = len(mails)
resp, lines, octets = server.retr(index)
# lines存储了邮件的原始文本的每一行,
# 可以获得整个邮件的原始文本:
msg_content = '\r\n'.join(lines)
# 稍后解析出邮件:
# msg = Parser().parsestr(msg_content)
# 可以根据邮件索引号直接从服务器删除邮件:
# server.dele(index)
# 关闭连接:
server.quit()
# retval = server.mime.base.MIMEBase()
# retval.set_payload(fd.read())
# email.encoders.encode_base64(retval)
# retval.add_header("Content-Disposition", "attachment", filename=filename)