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

yipeiwu_com5年前服务器

本文实例讲述了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程序设计有所帮助。

相关文章

PHP 高并发和大流量框架解决方案

一、高并发的概念在互联网时代,并发,高并发通常是指并发访问。也就是在某个时间点,有多少个访问同时到来。二、高并发架构相关概念1、QPS (每秒查询率) : 每秒钟请求或者查询的数量,在互联网领域,指每...

Python搭建FTP服务器的方法示例

Python搭建FTP服务器的方法示例

Python版本 3.6.2 使用的ftp包:pyftpdlib    pip install pyftpdlib就可以下载安装了 FTP协议下载上传文...

php实现从ftp服务器上下载文件树到本地电脑的程序

复制代码 代码如下:/* 用ftp_nlist()函授时,返回的数组值会有两种类型:因服务器不同而异 a:单独的文件名 b:包含目录的文件名。 如果挪用,请注意更改此处。 */ <...

python制作websocket服务器实例分享

python制作websocket服务器实例分享

一、开始的话   使用python简单的实现websocket服务器,可以在浏览器上实时显示远程服务器的日志信息。   之前做了一个web版的发布系统,但没实现在线看日志,每次发布版本后...

Session服务器配置指南与使用经验的深入解析

一.摘要所有Web程序都会使用Session保存数据. 使用独立的Session服务器可以解决负载均衡场景中的Session共享问题.本文介绍.NET平台下建立Session服务器的几种...