PyQt5+poplib+smtplib+python3.6邮件客户端Demo

目录:

准备

安装库:PyQt5,poplib,smtplib

邮件客户端主要功能包括接收邮件、发送邮件、邮件分类………,时间有限,只做出了发送邮件和接收邮件。

接收邮件主要依赖pop服务器,发送邮件依赖smtp服务器,例如网易163邮箱的pop服务器为pop.163.com,smtp服务器为smtp.163.com,发送端口为465。

目前测试网易邮箱没有问题,但是使用前确保打开了163的 POP3/SMTP/IMAP 服务,及开启 客户端授权密码

代码

包含三个界面,可使用QTdesigner设计GUI,也可直接使用我生成的界面py文件。三个界面如下:

主界面
发送邮件界面
登录界面

ui生成的代码分别是:

主界面ui –> pop3.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'pop3.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(551, 716)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(30, 70, 501, 231))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(5)
        self.tableWidget.setRowCount(0)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(2, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(3, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(4, item)
        self.tableWidget.horizontalHeader().setDefaultSectionSize(150)
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(30, 30, 61, 21))
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(360, 20, 81, 31))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(110, 20, 81, 31))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(454, 20, 81, 31))
        self.pushButton_3.setObjectName("pushButton_3")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(50, 320, 41, 21))
        self.label_2.setObjectName("label_2")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(90, 320, 441, 21))
        self.lineEdit.setObjectName("lineEdit")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(40, 360, 51, 21))
        self.label_3.setObjectName("label_3")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(90, 360, 441, 21))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(30, 440, 61, 21))
        self.label_4.setObjectName("label_4")
        self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_3.setGeometry(QtCore.QRect(90, 440, 441, 21))
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.label_5 = QtWidgets.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(50, 480, 41, 21))
        self.label_5.setObjectName("label_5")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(90, 480, 441, 131))
        self.textEdit.setObjectName("textEdit")
        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(230, 20, 81, 31))
        self.pushButton_4.setObjectName("pushButton_4")
        self.label_6 = QtWidgets.QLabel(self.centralwidget)
        self.label_6.setGeometry(QtCore.QRect(50, 400, 41, 21))
        self.label_6.setObjectName("label_6")
        self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_4.setGeometry(QtCore.QRect(90, 400, 441, 21))
        self.lineEdit_4.setObjectName("lineEdit_4")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 551, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        item = self.tableWidget.horizontalHeaderItem(0)
        item.setText(_translate("MainWindow", "标题"))
        item = self.tableWidget.horizontalHeaderItem(1)
        item.setText(_translate("MainWindow", "发送者"))
        item = self.tableWidget.horizontalHeaderItem(2)
        item.setText(_translate("MainWindow", "时间"))
        item = self.tableWidget.horizontalHeaderItem(3)
        item.setText(_translate("MainWindow", "发送邮箱"))
        item = self.tableWidget.horizontalHeaderItem(4)
        item.setText(_translate("MainWindow", "内容"))
        self.label.setText(_translate("MainWindow", "收信箱:"))
        self.pushButton.setText(_translate("MainWindow", "登录"))
        self.pushButton_2.setText(_translate("MainWindow", "发送邮件"))
        self.pushButton_3.setText(_translate("MainWindow", "退出"))
        self.label_2.setText(_translate("MainWindow", "标题:"))
        self.label_3.setText(_translate("MainWindow", "发送者:"))
        self.label_4.setText(_translate("MainWindow", "发送邮箱:"))
        self.label_5.setText(_translate("MainWindow", "正文:"))
        self.pushButton_4.setText(_translate("MainWindow", "刷新信箱"))
        self.label_6.setText(_translate("MainWindow", "时间:"))

发送邮件界面ui –> smtp.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'smtp.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(938, 405)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(70, 50, 81, 41))
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(150, 60, 321, 21))
        self.lineEdit.setObjectName("lineEdit")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(90, 100, 61, 41))
        self.label_2.setObjectName("label_2")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(150, 110, 321, 21))
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(70, 10, 81, 41))
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(70, 150, 81, 41))
        self.label_4.setObjectName("label_4")
        self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_4.setGeometry(QtCore.QRect(150, 160, 321, 21))
        self.lineEdit_4.setObjectName("lineEdit_4")
        self.label_5 = QtWidgets.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(70, 200, 81, 41))
        self.label_5.setObjectName("label_5")
        self.lineEdit_5 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_5.setGeometry(QtCore.QRect(150, 210, 321, 21))
        self.lineEdit_5.setObjectName("lineEdit_5")
        self.label_6 = QtWidgets.QLabel(self.centralwidget)
        self.label_6.setGeometry(QtCore.QRect(500, 0, 71, 41))
        self.label_6.setObjectName("label_6")
        self.lineEdit_6 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_6.setGeometry(QtCore.QRect(570, 10, 321, 20))
        self.lineEdit_6.setObjectName("lineEdit_6")
        self.label_7 = QtWidgets.QLabel(self.centralwidget)
        self.label_7.setGeometry(QtCore.QRect(500, 40, 61, 41))
        self.label_7.setObjectName("label_7")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(570, 50, 321, 191))
        self.textEdit.setObjectName("textEdit")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(650, 270, 81, 31))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(780, 270, 81, 31))
        self.pushButton_2.setObjectName("pushButton_2")
        self.label_8 = QtWidgets.QLabel(self.centralwidget)
        self.label_8.setGeometry(QtCore.QRect(150, 10, 211, 41))
        self.label_8.setObjectName("label_8")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 938, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "SMTP服务器:"))
        self.lineEdit.setText(_translate("MainWindow", "smtp.163.com"))
        self.label_2.setText(_translate("MainWindow", "端口号:"))
        self.lineEdit_2.setText(_translate("MainWindow", "465"))
        self.label_3.setText(_translate("MainWindow", "发件人账号:"))
        self.label_4.setText(_translate("MainWindow", "发件人昵称:"))
        self.label_5.setText(_translate("MainWindow", "收件人邮箱:"))
        self.label_6.setText(_translate("MainWindow", "邮件主题:"))
        self.label_7.setText(_translate("MainWindow", "邮件正文:"))
        self.pushButton.setText(_translate("MainWindow", "发送"))
        self.pushButton_2.setText(_translate("MainWindow", "取消"))
        self.label_8.setText(_translate("MainWindow", "TextLabel"))

登录界面ui–>idenfy.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'idenfy.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(384, 263)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(70, 50, 61, 41))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(70, 90, 61, 41))
        self.label_2.setObjectName("label_2")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(140, 60, 171, 21))
        self.lineEdit.setText("")
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(140, 100, 171, 21))
        self.lineEdit_2.setText("")
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(60, 10, 71, 41))
        self.label_3.setObjectName("label_3")
        self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_3.setGeometry(QtCore.QRect(140, 20, 171, 21))
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(100, 150, 71, 31))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(230, 150, 71, 31))
        self.pushButton_2.setObjectName("pushButton_2")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 384, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "邮箱账号:"))
        self.label_2.setText(_translate("MainWindow", "邮箱密码:"))
        self.label_3.setText(_translate("MainWindow", "pop3服务器:"))
        self.lineEdit_3.setText(_translate("MainWindow", "pop.163.com"))
        self.pushButton.setText(_translate("MainWindow", "登录"))
        self.pushButton_2.setText(_translate("MainWindow", "取消"))

主函数 emailServer.py

import idenfy
import pop3
import smtp
import sys
from PyQt5.QtWidgets import QMessageBox,QLineEdit,QApplication,QMainWindow,QAbstractItemView,QTableWidgetItem
import _thread
from PyQt5.QtCore import QCoreApplication
import poplib
import base64
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr
import datetime
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

app = QApplication(sys.argv)

is_Idenfy = False

user = {
    'pop3':None,
    'user':None,
    'pass':None
}


class Email_Server(object):
    def __init__(self,user_dic):
        # print(user_dic)
        self.user_mail = user_dic['user']
        self.password = user_dic['pass']
        self.pop_server = user_dic['pop3'] # pop.163.com
        self.Connect_Server()

    def Connect_Server(self):
        # 连接服务
        self.server = poplib.POP3(self.pop_server)
        self.server.user(self.user_mail)
        self.server.pass_(self.password)

    def _close_(self):
        # 关闭服务器资源
        self.server.close()

    def Get_Email_Count(self):
        # 获取邮件数目
        email_num, email_size = self.server.stat()
        # print("邮件数量: {0}, 消息大小:{1}Byte".format(email_num,email_size))
        return email_num

    def Get_Email_Data(self,email_row = None):
        rsp, msglines, msgsiz = self.server.retr(email_row)
        # print("服务器的响应: {0},\n原始邮件内容: {1},\n该封邮件所占字节大小: {2}".format(rsp, msglines, msgsiz))
        # 解析
        msg_content = b'\r\n'.join(msglines).decode('gbk')
        # print(msg_content)
        msg = Parser().parsestr(text=msg_content)
        # print(msg)
        self.msg = msg

        # 标题
        subject = self.msg['Subject']
        value, charset = decode_header(subject)[0]
        if charset:
            value = value.decode(charset)
        self.email_title = value
        print("标题: ",self.email_title)

        # 发送方
        hdr, addr = parseaddr(self.msg['From'])
        name, charset = decode_header(hdr)[0]
        if charset:
            name = name.decode(charset)
        self.email_name = name
        self.email_addr = addr
        print("发送方: ",name,addr)

        # 时间
        date = decode_header(self.msg.get('date'))
        utcstr = date[0][0].replace('+00:00', '')
        self.email_time = str(utcstr)
        # print(utcdatetime)

        # 内容
        try:
            content = self.msg.get_payload()
            content_charset = content[0].get_content_charset()  # 获取编码格式
            print(content_charset)
            text = content[0].as_string().split('base64')[-1]
            try:
                text_content = base64.b64decode(text).decode(content_charset)  # base64解码
            except:
                text_content = base64.b64decode(text).decode('gbk','ignore')  # base64解码
            # print(text_content)
            self.content = text_content
        except:
            self.content = " "


mail_Server = None


class Send_email(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.send_ui = smtp.Ui_MainWindow()
        self.send_ui.setupUi(self)
        self.send_ui.pushButton_2.clicked.connect(self.close)
        self.send_ui.pushButton.clicked.connect(self.Send)

    def Send(self):
        global mail_Server
        try:
            smtp_server = self.send_ui.lineEdit.text()
            smtp_port = self.send_ui.lineEdit_2.text()
            send_name = self.send_ui.lineEdit_4.text()
            recv_mail = self.send_ui.lineEdit_5.text()
            send_title = self.send_ui.lineEdit_6.text()
            send_text = self.send_ui.textEdit.toPlainText()
        except:
            QMessageBox.about(self, "Message", "填写完整信息")
        _thread.start_new_thread(self.Send_Email, (smtp_server,smtp_port,send_name,recv_mail,send_title,send_text,))

    def Send_Email(self,smtp_server,smtp_port,send_name,recv_mail,send_title,send_text):
        global mail_Server
        try:
            msg = MIMEText(send_text,'plain','utf-8')
            msg['From'] = formataddr([send_name,mail_Server.user_mail])#发件人邮箱昵称、发件人邮箱
            msg['To'] = formataddr(["star", recv_mail])  # 收件人邮箱昵称、收件人邮箱账号
            msg['Subject'] = send_title  # 邮件主题(标题
            server = smtplib.SMTP_SSL(smtp_server, int(smtp_port))  # 发送人邮箱SMTP服务器,端口号
            server.login(mail_Server.user_mail, mail_Server.password)  # 发件人邮箱账号、密码
            server.sendmail(mail_Server.user_mail, [recv_mail, ], msg.as_string())
            server.quit()
            QMessageBox.about(self, "Message", "发送成功!")
            self.close()
        except:
            QMessageBox.critical(self, "失败", "pop验证失败", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)


sendUi = None

class mainWin(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.main_ui = pop3.Ui_MainWindow()
        self.main_ui.setupUi(self)
        self.main_ui.pushButton_2.clicked.connect(self.Send)
        self.main_ui.pushButton_4.clicked.connect(self.Update)
        self.main_ui.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
        self.main_ui.tableWidget.itemClicked.connect(self.itemclick)
        self.main_ui.textEdit.document().setMaximumBlockCount(100)

    def itemclick(self):
        # print("嘤嘤嘤")
        now_current_row = self.main_ui.tableWidget.currentIndex().row()
        print(now_current_row)
        rowtitle = self.main_ui.tableWidget.item(now_current_row,0).text()
        # print(rowdata)
        rowsender = self.main_ui.tableWidget.item(now_current_row,1).text()
        etime = self.main_ui.tableWidget.item(now_current_row,2).text()
        rowaddr = self.main_ui.tableWidget.item(now_current_row,3).text()
        contents = self.main_ui.tableWidget.item(now_current_row,4).text()
        self.main_ui.textEdit.clear()
        self.main_ui.lineEdit.clear()
        self.main_ui.lineEdit_2.clear()
        self.main_ui.lineEdit_3.clear()
        self.main_ui.lineEdit_4.clear()
        _thread.start_new_thread(self.Dis_mail_data, (rowtitle,rowsender,rowaddr,contents,etime))

    def Dis_mail_data(self, title, sender, addr, cont,etime):
        self.main_ui.lineEdit.setText(title)
        self.main_ui.lineEdit_2.setText(sender)
        self.main_ui.lineEdit_3.setText(addr)
        self.main_ui.lineEdit_4.setText(etime)
        # print(len(cont))
        if len(cont) > 5000:
            self.main_ui.textEdit.append(" ")
        else:
            self.main_ui.textEdit.append(cont)


    def Update(self):
        global is_Idenfy,mail_Server
        if not is_Idenfy:
            QMessageBox.about(self, "Message", "请先登录")
        else:
            _thread.start_new_thread(self.Upthread, ())

    def Upthread(self):
        global mail_Server
        #清空列表
        allrownum = self.main_ui.tableWidget.rowCount()
        for i in range(allrownum):
            self.main_ui.tableWidget.removeRow(0)
        # 获取邮箱邮件数
        mail_count = mail_Server.Get_Email_Count()
        # 获取最新10条邮件
        for i in range(mail_count,mail_count-10,-1):
            mail_Server.Get_Email_Data(i)
            _thread.start_new_thread(self.Display, (mail_Server.email_title,mail_Server.email_name,mail_Server.email_addr,mail_Server.content,mail_Server.email_time,))

    def Display(self,title,name,addr,content,etime):
        rrow = self.main_ui.tableWidget.rowCount()
        self.main_ui.tableWidget.insertRow(rrow)
        self.main_ui.tableWidget.setItem(rrow, 0, QTableWidgetItem(title))
        self.main_ui.tableWidget.setItem(rrow, 1, QTableWidgetItem(name))
        self.main_ui.tableWidget.setItem(rrow, 2, QTableWidgetItem(str(etime)))
        self.main_ui.tableWidget.setItem(rrow, 3, QTableWidgetItem(addr))
        self.main_ui.tableWidget.setItem(rrow, 4, QTableWidgetItem(content))

    def Send(self):
        global is_Idenfy,user,sendUi
        if not is_Idenfy:
            QMessageBox.about(self, "Message", "请先登录")
        else:
            sendUi = Send_email()
            sendUi.send_ui.label_8.setText(user['user'])
            sendUi.show()


class loginWin(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.login_ui = idenfy.Ui_MainWindow()
        self.login_ui.setupUi(self)
        self.login_ui.pushButton_2.clicked.connect(self.close)
        self.login_ui.pushButton.clicked.connect(self.Login)
        self.login_ui.lineEdit_2.setEchoMode(QLineEdit.Password)

    def Login(self):
        print("login....")
        global user,mail_Server,is_Idenfy
        user['user'] = str(self.login_ui.lineEdit.text())
        user['pass'] = str(self.login_ui.lineEdit_2.text())
        user['pop3'] = str(self.login_ui.lineEdit_3.text())
        # print(user)
        try:
            mail_Server = Email_Server(user)
            print("连接成功.......")
            QMessageBox.about(self, "连接成功", "验证成功!")
            self.close()
            is_Idenfy = True
        except:
            print("pop验证失败!")
            QMessageBox.critical(self, "失败", "pop验证失败", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)


if __name__ == '__main__':
    ui = mainWin()
    loginui = loginWin()
    ui.main_ui.pushButton.clicked.connect(loginui.show)
    ui.main_ui.pushButton_3.clicked.connect(QApplication.quit)
    ui.show()

    sys.exit(app.exec_())

运行演示

运行emailServer.py后出现主界面,在进行操作前请先登录。

登录

登录后点击刷新信箱将会显出最新的10封邮件,点击表格任意行将会在下方显示邮件详情

邮件详情

点击发送邮件可弹出发送邮件窗口,填写必要信息后点击发送,稍作等待会有弹窗提示发送成功或失败:

发送邮件
成功提示

发送成功后收件人会收到邮件。

收到邮件

目前客户端存在一些问题:1)无法添加图片或附件。2)发送邮件成功后客户端可能会被关闭。