如何使用poplib
使用poplib接收QQ邮件
注意:使用第三方邮件客户端,需要使用短信开通并获取授权码。
http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
# -*- coding:utf-8 -*-
#
import re
import poplib
import Tkinter
class Window:
def __init__(self, root):
label1 = Tkinter.Label(root, text = 'POP3')
label2 = Tkinter.Label(root, text = 'Port')
label3 = Tkinter.Label(root, text = '用户名')
label4 = Tkinter.Label(root, text = '密码')
label1.place(x = 5, y = 5)
label2.place(x = 5, y = 30)
label3.place(x = 5, y = 55)
label4.place(x = 5, y = 80)
self.entryPOP = Tkinter.Entry(root)
self.entryPort = Tkinter.Entry(root)
self.entryUser = Tkinter.Entry(root)
self.entryPass = Tkinter.Entry(root, show='*')
self.entryPOP.insert(Tkinter.END, "pop.qq.com")
self.entryPort.insert(Tkinter.END, "995")
self.entryUser.insert(Tkinter.END, "********@qq.com")# ******@qq.com为测试邮箱,下同
self.entryPass = Tkinter.Entry(root, show='*')
self.entryPass.insert(Tkinter.END, "****************") # 填入“授权码”
self.entryPOP.place(x = 40, y = 5)
self.entryPort.place(x = 40, y = 30)
self.entryUser.place(x = 40, y = 55)
self.entryPass.place(x = 40, y = 80)
self.get=Tkinter.Button(root, text = '收取邮件', command = self.Get)
self.get.place(x=60, y=120)
self.text = Tkinter.Text(root)
self.text.place(y=150)
def Get(self):
try:
host = self.entryPOP.get()
port = int(self.entryPort.get())
user = self.entryUser.get()
pw = self.entryPass.get()
# pop = poplib.POP3(host)
pop = poplib.POP3_SSL(host, port)
pop.set_debuglevel(1)
pop.user(user)
pop.pass_(pw)
stat = pop.stat()
self.text.insert(Tkinter.END,'Status: %d message(s), %d bytes\n' % stat)
rx_headers = re.compile(r"^(From|To|Subject)")
for n in range(stat[0]):
response, lines, bytes = pop.top(n+1, 10)
self.text.insert(Tkinter.END,"Message %d (%d bytes)\n" % (n+1, bytes))
self.text.insert(Tkinter.END, "-" * 30 + '\n')
self.text.insert(Tkinter.END, "\n".join(filter(rx_headers.match, lines)))
self.text.insert(Tkinter.END, "\n")
self.text.insert(Tkinter.END, "-" * 30 + "\n")
except:
self.text.insert(Tkinter.END, '接收错误\n')
root = Tkinter.Tk()
window = Window(root)
root.minsize(600, 480)
root.mainloop()
1. 客户端界面
2. 交互信息
3. 收信信息
什么是poplib
Python中poplib模块提供了对POP3协议的支持。
使用POP3协议可以登录E-mail收取邮件。