本文實(shí)例講述了python實(shí)現(xiàn)的接收郵件功能。分享給大家供大家參考,具體如下:
一 簡(jiǎn)介
本代碼實(shí)現(xiàn)從網(wǎng)易POP3服務(wù)器接收郵件
二 代碼
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,'接收錯(cuò)誤\n')
root =tkinter.Tk()
window=Window(root)
root.mainloop()
三 運(yùn)行結(jié)果
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

