十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要介绍Python如何实现发送与接收邮件,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联公司是一家专业的成都网站建设公司,我们专注做网站、网站建设、网络营销、企业网站建设,买链接,广告投放平台为企业客户提供一站式建站解决方案,能带给客户新的互联网理念。从网站结构的规划UI设计到用户体验提高,创新互联力求做到尽善尽美。一、发送邮件
这里实现给网易邮箱发送邮件功能:
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='发件人') 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='发送邮件',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,'发送错误\n') root =tkinter.Tk() window=Window(root) root.minsize(600,400) root.mainloop()
运行结果
二、接收邮件
这里实现从网易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如何实现发送与接收邮件”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!