python實(shí)現(xiàn)的發(fā)郵件功能示例
本文實(shí)例講述了python實(shí)現(xiàn)的發(fā)郵件功能。分享給大家供大家參考,具體如下:
一 簡(jiǎn)介
本應(yīng)用實(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é)果
更多關(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ì)有所幫助。
相關(guān)文章
python使用正則搜索字符串或文件中的浮點(diǎn)數(shù)代碼實(shí)例
這篇文章主要介紹了python使用正則搜索字符串或文件中的浮點(diǎn)數(shù)代碼實(shí)例,同時(shí)包含一個(gè)讀寫(xiě)到文件功能,需要的朋友可以參考下2014-07-07python實(shí)現(xiàn)根據(jù)月份和日期得到星座的方法
這篇文章主要介紹了python實(shí)現(xiàn)根據(jù)月份和日期得到星座的方法,涉及Python操作字符串及數(shù)組的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03Python中生成隨機(jī)密碼的多種超實(shí)用實(shí)例
隨機(jī)密碼生成器是一種常見(jiàn)的工具,用于生成強(qiáng)密碼,下面這篇文章主要給大家介紹了關(guān)于Python中生成隨機(jī)密碼的多種超實(shí)用實(shí)例,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05多個(gè)版本的python共存時(shí)使用pip的正確做法
這篇文章主要介紹了多版本python共存時(shí)使用pip的正確做法,幫助有多個(gè)python版本需求的人可以正確的導(dǎo)包,感興趣的朋友可以了解下2020-10-10python實(shí)現(xiàn)系統(tǒng)狀態(tài)監(jiān)測(cè)和故障轉(zhuǎn)移實(shí)例方法
這篇文章主要介紹了用python實(shí)現(xiàn)系統(tǒng)狀態(tài)監(jiān)測(cè)和故障轉(zhuǎn)移的代碼方法2013-11-11