python实现的接收邮件功能示例【基于网易POP3服务器】

yipeiwu_com6年前服务器

本文实例讲述了python实现的接收邮件功能。分享给大家供大家参考,具体如下:

一 简介

本代码实现从网易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相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

Python基于twisted实现简单的web服务器

Python基于twisted实现简单的web服务器

本文实例讲述了Python基于twisted实现简单的web服务器,分享给大家供大家参考。具体方法如下: 1. 新建htm文件夹,在这个文件夹中放入显示的网页文件 2. 在htm文件夹的...

PHP中实现生成静态文件的方法缓解服务器压力

互联网快速普及的现在社会,越来越多的人会在一个web应用上进行交流,因而导致服务器与数据库访问压力与日俱增,这边就需要进行一些优化,譬如增加缓存、二级缓存、动态网页静态化以及其他的高端技...

python实现的文件同步服务器实例

本文实例讲述了python实现的文件同步服务器。分享给大家供大家参考。具体实现方法如下: 服务端使用asyncore, 收到文件后保存到本地。 客户端使用pyinotify监视目录的变化...

在Linux系统的服务器上隐藏PHP版本号的方法

通常,大多数默认设置安装的web服务器存在信息泄露,这其中之一就是PHP。PHP 是如今流行的服务端html嵌入式语言(之一?)。在如今这个充满挑战的时代,有许多攻击者会尝试发现你服务端...

在Apache服务器上同时运行多个Django程序的方法

在同一个 Apache 实例中运行多个 Django 程序是完全可能的。 当你是一个独立的 Web 开发人员并有多个不同的客户时,你可能会想这么做。 只要像下面这样使用 VirtualH...