本文實例講述了python實現的接收郵件功能。分享給大家供大家參考,具體如下:
一 簡介
本代碼實現從網易POP3服務器接收郵件
二 代碼
import poplib
import re
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.entryPort.insert(tkinter.END,'110')
self.entryPop.place(x=50,y=5)
self.entryPort.place(x=50,y=30)
self.entryUser.place(x=50,y=55)
self.entryPass.place(x=50,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.user(user)
pop.pass_(pw)
stat=pop.stat()
self.text.insert(tkinter.END,'Staus:%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')
str_lines=[]
for l in lines:
str_lines.append(l.decode(encoding = 'utf-8'))
self.text.insert(tkinter.END,"\n".join(filter(rx_headers.match,str_lines)))
self.text.insert(tkinter.END,'\n')
self.text.insert(tkinter.END,"-"*30+'\n')
except Exception as e:
self.text.insert(tkinter.END,'接收錯誤\n')
root =tkinter.Tk()
window=Window(root)
root.mainloop()
三 運行結果
更多關于Python相關內容可查看本站專題:《Python Socket編程技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

