公司把126这类的邮箱网站都封了,正好现在无事,加之,算是一个对这俩周学习Qt的一个总结吧。遂写了这么一个简易的通过POP3协议接收126邮件的客户端。
源代码:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'pop3_mail_client.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from email.parser import Parser
from email.header import decode_header
from email.utils import parseaddr
from email.policy import default
import sys
import json
import poplib
import datetime
class Ui_frmPOP3Mail(object):
def setupUi(self, frmPOP3Mail):
frmPOP3Mail.setObjectName("frmPOP3Mail")
frmPOP3Mail.resize(1176, 718)
self.centralwidget = QtWidgets.QWidget(frmPOP3Mail)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 101, 81))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.lblPOP3Addr = QtWidgets.QLabel(self.verticalLayoutWidget)
self.lblPOP3Addr.setObjectName("lblPOP3Addr")
self.verticalLayout.addWidget(self.lblPOP3Addr)
self.lblMailAcct = QtWidgets.QLabel(self.verticalLayoutWidget)
self.lblMailAcct.setObjectName("lblMailAcct")
self.verticalLayout.addWidget(self.lblMailAcct)
self.lblMailPwd = QtWidgets.QLabel(self.verticalLayoutWidget)
self.lblMailPwd.setObjectName("lblMailPwd")
self.verticalLayout.addWidget(self.lblMailPwd)
self.verticalLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(110, 10, 461, 85))
self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.txtPOP3Addr = QtWidgets.QLineEdit(self.verticalLayoutWidget_2)
self.txtPOP3Addr.setObjectName("txtPOP3Addr")
self.verticalLayout_2.addWidget(self.txtPOP3Addr)
self.txtMailAcct = QtWidgets.QLineEdit(self.verticalLayoutWidget_2)
self.txtMailAcct.setObjectName("txtMailAcct")
self.verticalLayout_2.addWidget(self.txtMailAcct)
self.txtMailPwd = QtWidgets.QLineEdit(self.verticalLayoutWidget_2)
self.txtMailPwd.setObjectName("txtMailPwd")
self.verticalLayout_2.addWidget(self.txtMailPwd)
self.btnRefresh = QtWidgets.QPushButton(self.centralwidget)
self.btnRefresh.setGeometry(QtCore.QRect(620, 40, 93, 28))
self.btnRefresh.setObjectName("btnRefresh")
self.tblMail = QtWidgets.QTableView(self.centralwidget)
self.tblMail.setGeometry(QtCore.QRect(10, 110, 1151, 541))
self.tblMail.setObjectName("tblMail")
self.tblMail.horizontalHeader().setStretchLastSection(False)
self.tblMail.setEditTriggers(QAbstractItemView.SelectedClicked)
self.btnNewEmail = QtWidgets.QPushButton(self.centralwidget)
self.btnNewEmail.setGeometry(QtCore.QRect(810, 40, 93, 28))
self.btnNewEmail.setObjectName("btnNewEmail")
frmPOP3Mail.setCentralWidget(self.centralwidget)
self.retranslateUi(frmPOP3Mail)
QtCore.QMetaObject.connectSlotsByName(frmPOP3Mail)
self.mailModel = QtGui.QStandardItemModel()
self.mailModel.setHorizontalHeaderLabels(['From', 'Subject', 'Received Date', 'Message ID'])
self.tblMail.setModel(self.mailModel)
self.tblMail.setColumnWidth(0,335)
self.tblMail.setColumnWidth(1,552)
self.tblMail.setColumnWidth(2,247)
self.tblMail.setColumnWidth(3,0)
self.btnRefresh.clicked.connect(self.loadNewMails)
self.mailList = {}
self.setMailDetailDialog_UI()
self.tblMail.setContextMenuPolicy(QtCore.Qt.ContextMenuPolicy.ActionsContextMenu)
self.maildetailAction = QAction("Mail Detail", self.tblMail)
self.maildetailAction.triggered.connect(self.showMailDetail)
self.tblMail.addAction(self.maildetailAction)
def setMailDetailDialog_UI(self):
self.frmMailDetail = QtWidgets.QDialog(self.centralwidget)
self.frmMailDetail.resize(763, 492)
self.frmMailDetail.setModal(True)
self.frmMailDetail.hide()
self.btnDialogOK = QDialogButtonBox(self.frmMailDetail)
self.btnDialogOK.setObjectName(u"btnDialogOK")
self.btnDialogOK.setGeometry(QRect(350, 450, 71, 32))
self.btnDialogOK.setOrientation(Qt.Horizontal)
self.btnDialogOK.setStandardButtons(QDialogButtonBox.Ok)
self.verticalLayoutWidget = QWidget(self.frmMailDetail)
self.verticalLayoutWidget.setObjectName(u"verticalLayoutWidget")
self.verticalLayoutWidget.setGeometry(QRect(10, 10, 91, 80))
self.vlytDialogLabel = QVBoxLayout(self.verticalLayoutWidget)
self.vlytDialogLabel.setObjectName(u"vlytDialogLabel")
self.vlytDialogLabel.setContentsMargins(0, 0, 0, 0)
self.lblDialogFrom = QLabel(self.verticalLayoutWidget)
self.lblDialogFrom.setObjectName(u"lblDialogFrom")
self.vlytDialogLabel.addWidget(self.lblDialogFrom)
self.lblDialogSubject = QLabel(self.verticalLayoutWidget)
self.lblDialogSubject.setObjectName(u"lblDialogSubject")
self.vlytDialogLabel.addWidget(self.lblDialogSubject)
self.verticalLayoutWidget_2 = QWidget(self.frmMailDetail)
self.verticalLayoutWidget_2.setObjectName(u"verticalLayoutWidget_2")
self.verticalLayoutWidget_2.setGeometry(QRect(100, 10, 511, 80))
self.vlytDialogText = QVBoxLayout(self.verticalLayoutWidget_2)
self.vlytDialogText.setObjectName(u"vlytDialogText")
self.vlytDialogText.setContentsMargins(0, 0, 0, 0)
self.txtDialogFrom = QLineEdit(self.verticalLayoutWidget_2)
self.txtDialogFrom.setObjectName(u"txtDialogFrom")
self.txtDialogFrom.setReadOnly(True)
self.vlytDialogText.addWidget(self.txtDialogFrom)
self.txtDialogSubject = QLineEdit(self.verticalLayoutWidget_2)
self.txtDialogSubject.setObjectName(u"txtDialogSubject")
self.txtDialogSubject.setReadOnly(True)
self.vlytDialogText.addWidget(self.txtDialogSubject)
self.txtDialogMailDetail = QTextEdit(self.frmMailDetail)
self.txtDialogMailDetail.setObjectName(u"txtDialogMailDetail")
self.txtDialogMailDetail.setGeometry(QRect(10, 100, 741, 341))
self.btnDialogOK.clicked.connect(self.hideMailDetailDialog)
self.frmMailDetail.setWindowTitle(QCoreApplication.translate("frmMailDetail", u"Mail Detail", None))
self.lblDialogFrom.setText(QCoreApplication.translate("frmMailDetail", u"From", None))
self.lblDialogSubject.setText(QCoreApplication.translate("frmMailDetail", u"Subject", None))
QtCore.QMetaObject.connectSlotsByName(self.frmMailDetail)
def retranslateUi(self, frmPOP3Mail):
_translate = QtCore.QCoreApplication.translate
frmPOP3Mail.setWindowTitle(_translate("frmPOP3Mail", "POP3 Mail Client"))
self.lblPOP3Addr.setText(_translate("frmPOP3Mail", "POP3 Address"))
self.lblMailAcct.setText(_translate("frmPOP3Mail", "Mail Account"))
self.lblMailPwd.setText(_translate("frmPOP3Mail", "Password"))
self.btnRefresh.setText(_translate("frmPOP3Mail", "Refresh"))
self.btnNewEmail.setText(_translate("frmPOP3Mail", "New Email"))
def show(self, Form):
Form.show()
def loadNewMails(self):
user = self.txtMailAcct.text()
password=self.txtMailPwd.text()
address=self.txtPOP3Addr.text()
Mailbox = poplib.POP3_SSL(address, '995')
Mailbox.user(user)
Mailbox.pass_(password)
NumofMessages = len(Mailbox.list()[1])
for i in range(NumofMessages):
Mailbox.rset()
lines=Mailbox.retr(i+1)[1]
msg=b'\n'.join(lines).decode('utf-8')
msg_formatted=Parser(policy=default).parsestr(msg)
email_id=msg_formatted.get("message-id")
email_from = msg_formatted.get('From')
email_subject = msg_formatted.get('Subject')
email_receive_date = msg_formatted.get("date")
email_receive_date_c=self.date_convert(email_receive_date)
email_body=msg_formatted.get_body().get_content()
self.mailList[email_id] = [email_from, email_subject, email_receive_date_c, email_body]
self.mailModel.appendRow([QStandardItem(email_from),
QStandardItem(email_subject),
QStandardItem(str(email_receive_date_c)),
QStandardItem(email_id)])
Mailbox.quit()
def date_convert(self, date_time):
format = '%a, %d %b %Y %H:%M:%S %z'
datetime_str = datetime.datetime.strptime(date_time, format)
return datetime_str
def hideMailDetailDialog(self):
self.frmMailDetail.hide()
def showMailDetail(self):
select_item=self.tblMail.currentIndex()
email_id=self.mailModel.item(select_item.row(),3).text()
self.txtDialogFrom.setText(self.mailList[email_id][0])
self.txtDialogSubject.setText(self.mailList[email_id][1])
self.txtDialogMailDetail.setHtml(self.mailList[email_id][3])
self.frmMailDetail.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
Form=QtWidgets.QMainWindow()
main_win = Ui_frmPOP3Mail()
main_win.setupUi(Form)
main_win.show(Form)
sys.exit(app.exec())
截图: