這篇文章主要介紹Python如何實(shí)現(xiàn)發(fā)送與接收郵件,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)公司是一家專業(yè)的成都網(wǎng)站建設(shè)公司,我們專注做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)絡(luò)營(yíng)銷、企業(yè)網(wǎng)站建設(shè),買鏈接,廣告投放平臺(tái)為企業(yè)客戶提供一站式建站解決方案,能帶給客戶新的互聯(lián)網(wǎng)理念。從網(wǎng)站結(jié)構(gòu)的規(guī)劃UI設(shè)計(jì)到用戶體驗(yàn)提高,創(chuàng)新互聯(lián)力求做到盡善盡美。一、發(fā)送郵件
這里實(shí)現(xiàn)給網(wǎng)易郵箱發(fā)送郵件功能:
import smtplib import tkinter class Window: def __init__(self,root): label1 = tkinter.Label(root,text='SMTP') label2 = tkinter.Label(root,text='Port') label3 = tkinter.Label(root,text='用戶名') label4 = tkinter.Label(root,text='密碼') label5 = tkinter.Label(root,text='收件人') label6 = tkinter.Label(root,text='主題') label7 = tkinter.Label(root,text='發(fā)件人') label1.place(x=5,y=5) label2.place(x=5,y=30) label3.place(x=5,y=55) label4.place(x=5,y=80) label5.place(x=5,y=105) label6.place(x=5,y=130) label7.place(x=5,y=155) self.entryPop = tkinter.Entry(root) self.entryPort = tkinter.Entry(root) self.entryUser = tkinter.Entry(root) self.entryPass = tkinter.Entry(root,show = '*') self.entryTo = tkinter.Entry(root) self.entrySub = tkinter.Entry(root) self.entryFrom = tkinter.Entry(root) self.entryPort.insert(tkinter.END,'25') 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.entryTo.place(x=50,y=105) self.entrySub.place(x=50,y=130) self.entryFrom.place(x=50,y=155) self.get = tkinter.Button(root,text='發(fā)送郵件',command = self.Get) self.get.place(x=60,y=180) self.text=tkinter.Text(root) self.text.place(y=220) def Get(self): try: host = self.entryPop.get() port =int(self.entryPort.get()) user = self.entryUser.get() pw = self.entryPass.get() fromaddr = self.entryFrom.get() toaddr=self.entryTo.get() subject=self.entrySub.get() text = self.text.get(1.0,tkinter.END) msg =("From:%s\nTo:%s\nSubject:%s\n\n" % (fromaddr,toaddr,subject)) msg = msg+text smtp=smtplib.SMTP(host,port) smtp.set_debuglevel(1) smtp.login(user,pw) smtp.sendmail(fromaddr,toaddr,msg) smtp.quit() except Exception as e: self.text.insert(tkinter.END,'發(fā)送錯(cuò)誤\n') root =tkinter.Tk() window=Window(root) root.minsize(600,400) root.mainloop()
運(yùn)行結(jié)果
二、接收郵件
這里實(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é)果
以上是“Python如何實(shí)現(xiàn)發(fā)送與接收郵件”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文名稱:Python如何實(shí)現(xiàn)發(fā)送與接收郵件-創(chuàng)新互聯(lián)
轉(zhuǎn)載來(lái)于:http://muchs.cn/article22/iohjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、響應(yīng)式網(wǎng)站、企業(yè)網(wǎng)站制作、網(wǎng)站營(yíng)銷、電子商務(wù)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容