Python實(shí)現(xiàn)發(fā)送與接收郵件的方法詳解
本文實(shí)例講述了Python實(shí)現(xiàn)發(fā)送與接收郵件的方法。分享給大家供大家參考,具體如下:
一、發(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ā)送錯誤\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,'接收錯誤\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文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
- python登錄pop3郵件服務(wù)器接收郵件的方法
- python發(fā)送郵件接收郵件示例分享
- 利用python發(fā)送和接收郵件
- Python接收Gmail新郵件并發(fā)送到gtalk的方法
- 在Python中使用poplib模塊收取郵件的教程
- 在Python的Flask框架下收發(fā)電子郵件的教程
- 簡單實(shí)現(xiàn)python收發(fā)郵件功能
- Python使用smtp和pop簡單收發(fā)郵件完整實(shí)例
- python smtplib模塊自動收發(fā)郵件功能(一)
- python smtplib模塊自動收發(fā)郵件功能(二)
- Python使用POP3和SMTP協(xié)議收發(fā)郵件的示例代碼
- python實(shí)現(xiàn)的接收郵件功能示例【基于網(wǎng)易POP3服務(wù)器】
相關(guān)文章
python中必會的四大高級數(shù)據(jù)類型(字符,元組,列表,字典)
這篇文章主要介紹了python中必會的四大高級數(shù)據(jù)類型(字符,元組,列表,字典),本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05使用Python解析JSON數(shù)據(jù)的基本方法
這篇文章主要介紹了使用Python解析JSON數(shù)據(jù)的基本方法,是Python入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-10-10python讀取圖片的方式,以及將圖片以三維數(shù)組的形式輸出方法
今天小編就為大家分享一篇python讀取圖片的方式,以及將圖片以三維數(shù)組的形式輸出方法,具有好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07python自然語言編碼轉(zhuǎn)換模塊codecs介紹
這篇文章主要介紹了python自然語言編碼轉(zhuǎn)換模塊codecs介紹,codecs專門用作編碼轉(zhuǎn)換,通過它的接口是可以擴(kuò)展到其他關(guān)于代碼方面的轉(zhuǎn)換,需要的朋友可以參考下2015-04-04python機(jī)器學(xué)習(xí)之神經(jīng)網(wǎng)絡(luò)
這篇文章主要介紹了python機(jī)器學(xué)習(xí)之神經(jīng)網(wǎng)絡(luò),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下2021-04-04python numpy之np.random的隨機(jī)數(shù)函數(shù)使用介紹
這篇文章主要介紹了python numpy之np.random的隨機(jī)數(shù)函數(shù)使用介紹,需要的朋友可以參考下2019-10-10基于PyQT實(shí)現(xiàn)區(qū)分左鍵雙擊和單擊
這篇文章主要介紹了基于PyQT實(shí)現(xiàn)區(qū)分左鍵雙擊和單擊,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05