NJUPT《网络编程》

作者: Du1in9 | 来源:发表于2022-06-02 18:24 被阅读0次

    尝试编写一个界面化的软件,进行与QQ邮箱的交互。


    授权口令的获取方式
    调试代码如下:
    # test.py
    
    import smtplib
    from email.mime.text import MIMEText
    
    # 构造一个纯文本邮件
    msg = MIMEText("(可编辑)", "plain", "utf-8")
    
    # 使用SMTP发送邮件
    # 输入发件人Email地址课口令
    from_addr = input("请输入发件人邮箱地址From:") #自己的邮箱
    from_pwd = input("请输入发件人邮箱口令Password::") #邮箱授权口令
    # 收件人Email地址
    to_addr = input("请输入收件人邮箱地址To:") #别人的邮箱
    # 输入SMTP服务器地址:
    smtp_server = input('请输入邮箱服务器地址SMTP server: ') #smtp.qq.com
    
    smtp.qq.com
    
    
    server = smtplib.SMTP_SSL(smtp_server, 465)  # SMTP协议加密端口是25
    server.set_debuglevel(1)
    server.login(from_addr, from_pwd)
    server.sendmail(from_addr, [to_addr], msg.as_string())
    # 停止服务
    server.quit()
    
    # test2.py
    
    import smtplib
    from email.header import Header
    from email.utils import parseaddr, formataddr
    from email.mime.text import MIMEText
    
    
    def _format_addr(s):
        name, addr = parseaddr(s)
        return formataddr((Header(name, "utf-8").encode(), addr))
    
    
    # 使用SMTP发送邮件
    # 输入发件人Email地址课口令
    from_addr = input("请输入发件人邮箱地址From:")
    from_pwd = input("请输入发件人邮箱口令Password:")
    # 收件人Email地址
    to_addr = input("请输入收件人邮箱地址To:")
    # 输入SMTP服务器地址:
    smtp_server = input('请输入邮箱服务器地址SMTP server: ')
    
    content = '''
        桃之夭夭,灼灼其华。之子于归,宜其室家。
        桃之夭夭,有蕡其实。之子于归,宜其家室。
        桃之夭夭,其叶蓁蓁。之子于归,宜其家人。
    '''
    
    # 实例化MIMEText对象
    msg = MIMEText(content, "plain", "utf-8")
    # 发件人
    msg['From'] = _format_addr(u'(可编辑)<%s>' % from_addr)
    # 收件人
    msg["To"] = _format_addr(u'(可编辑)<%s>' % to_addr)
    # 主题
    msg["Subject"] = Header(u'(可编辑)', 'utf-8').encode()
    
    server = smtplib.SMTP_SSL(smtp_server, 465)  # SMTP协议加密端口是25
    server.set_debuglevel(1)
    server.login(from_addr, from_pwd)
    server.sendmail(from_addr, [to_addr], msg.as_string())
    # 停止服务
    server.quit()
    
    # test3.py
    
    from email.mime.multipart import MIMEMultipart
    from email.header import Header
    from email.mime.text import MIMEText
    from email import encoders
    import smtplib
    from email.mime.base import MIMEBase
    from email.utils import formataddr, parseaddr
    
    # 使用SMTP发送邮件
    from_addr = input("请输入发件人邮箱地址From:")
    from_pwd = input("请输入发件人邮箱口令Password:")
    to_addr = input("请输入收件人邮箱地址To:")
    smtp_server = input('请输入邮箱服务器地址SMTP server: ')
    
    
    def _format_addr(s):
        name, addr = parseaddr(s)
        return formataddr((Header(name, "utf-8").encode(), addr))
    
    
    # 邮件对象:
    msg = MIMEMultipart()
    msg['From'] = _format_addr(u'(可编辑)<%s>' % from_addr)
    msg["To"] = _format_addr(u'(可编辑)<%s>' % to_addr)
    msg["Subject"] = Header(u'(可编辑)', 'utf-8').encode()
    
    # 邮件正文是MIMEText:
    msg.attach(MIMEText('<html><body><h1>Hello</h1>' +
                        '<p><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg'
                        '%2F1113%2F0F220092919%2F200F2092919-10-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size'
                        '=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1656571269&t=9125481bb6d20228176e97db3e22e1ab"></p>' +
                        '</body></html>', 'html', 'utf-8'))
    
    # 添加附件就是加上一个MIMEBase,从本地读取一个图片:
    with open('./test.jpg', "rb") as fp:
        mime = MIMEBase("image", "jpg", filename="test.jpg")
        mime.add_header('Content-Disposition', 'attachment', filename='test.jpg')
        mime.add_header('Content-ID', '<0>')
        mime.add_header('X-Attachment-Id', '0')
        mime.set_payload(fp.read())
        encoders.encode_base64(mime)
        msg.attach(mime)
    
    server = smtplib.SMTP_SSL(smtp_server, 465)
    server.set_debuglevel(1)
    server.login(from_addr, from_pwd)
    server.sendmail(from_addr, [to_addr], msg.as_string())
    # 停止服务
    server.quit()
    

    然后用 QtPy5 进行界面设计,并编写逻辑代码:

    # demo.py
    
    # -*- coding: utf-8 -*-
    # Form implementation generated from reading ui file 'demo.ui'
    # Created by: PyQt5 UI code generator 5.15.4
    # 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
    
    
    class Ui_QQemail(object):
        def setupUi(self, QQemail):
            QQemail.setObjectName("QQemail")
            QQemail.resize(874, 405)
            self.centralwidget = QtWidgets.QWidget(QQemail)
            self.centralwidget.setObjectName("centralwidget")
            self.send_button = QtWidgets.QPushButton(self.centralwidget)
            self.send_button.setGeometry(QtCore.QRect(650, 300, 121, 41))
            self.send_button.setObjectName("send_button")
            self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
            self.lineEdit.setGeometry(QtCore.QRect(140, 40, 181, 31))
            self.lineEdit.setObjectName("lineEdit")
            self.label = QtWidgets.QLabel(self.centralwidget)
            self.label.setGeometry(QtCore.QRect(40, 50, 72, 15))
            self.label.setObjectName("label")
            self.label_2 = QtWidgets.QLabel(self.centralwidget)
            self.label_2.setGeometry(QtCore.QRect(40, 120, 72, 15))
            self.label_2.setObjectName("label_2")
            self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
            self.lineEdit_2.setGeometry(QtCore.QRect(140, 110, 181, 31))
            self.lineEdit_2.setObjectName("lineEdit_2")
            self.label_3 = QtWidgets.QLabel(self.centralwidget)
            self.label_3.setGeometry(QtCore.QRect(40, 180, 72, 15))
            self.label_3.setObjectName("label_3")
            self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
            self.lineEdit_3.setGeometry(QtCore.QRect(140, 170, 181, 31))
            self.lineEdit_3.setObjectName("lineEdit_3")
            self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
            self.lineEdit_4.setGeometry(QtCore.QRect(140, 230, 181, 31))
            self.lineEdit_4.setText("")
            self.lineEdit_4.setObjectName("lineEdit_4")
            self.label_4 = QtWidgets.QLabel(self.centralwidget)
            self.label_4.setGeometry(QtCore.QRect(40, 240, 72, 15))
            self.label_4.setObjectName("label_4")
            self.label_5 = QtWidgets.QLabel(self.centralwidget)
            self.label_5.setGeometry(QtCore.QRect(40, 300, 161, 16))
            self.label_5.setObjectName("label_5")
            self.fontComboBox = QtWidgets.QFontComboBox(self.centralwidget)
            self.fontComboBox.setGeometry(QtCore.QRect(588, 50, 221, 22))
            self.fontComboBox.setObjectName("fontComboBox")
            self.dateEdit = QtWidgets.QDateEdit(self.centralwidget)
            self.dateEdit.setGeometry(QtCore.QRect(360, 50, 121, 22))
            self.dateEdit.setObjectName("dateEdit")
            self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
            self.plainTextEdit.setGeometry(QtCore.QRect(363, 96, 451, 191))
            self.plainTextEdit.setObjectName("plainTextEdit")
            self.timeEdit = QtWidgets.QTimeEdit(self.centralwidget)
            self.timeEdit.setGeometry(QtCore.QRect(490, 50, 91, 22))
            self.timeEdit.setObjectName("timeEdit")
            self.send_button_2 = QtWidgets.QPushButton(self.centralwidget)
            self.send_button_2.setGeometry(QtCore.QRect(390, 300, 121, 41))
            self.send_button_2.setObjectName("send_button_2")
            self.spinBox = QtWidgets.QSpinBox(self.centralwidget)
            self.spinBox.setGeometry(QtCore.QRect(140, 290, 121, 31))
            self.spinBox.setObjectName("spinBox")
            QQemail.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(QQemail)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 874, 26))
            self.menubar.setObjectName("menubar")
            self.menu = QtWidgets.QMenu(self.menubar)
            self.menu.setObjectName("menu")
            QQemail.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(QQemail)
            self.statusbar.setObjectName("statusbar")
            QQemail.setStatusBar(self.statusbar)
            self.menubar.addAction(self.menu.menuAction())
    
            self.retranslateUi(QQemail)
            QtCore.QMetaObject.connectSlotsByName(QQemail)
    
        def retranslateUi(self, QQemail):
            _translate = QtCore.QCoreApplication.translate
            QQemail.setWindowTitle(_translate("QQemail", "QQ Email"))
            self.send_button.setText(_translate("QQemail", "发送"))
            self.label.setText(_translate("QQemail", "发送邮箱"))
            self.label_2.setText(_translate("QQemail", "授权口令"))
            self.label_3.setText(_translate("QQemail", "接收邮箱"))
            self.label_4.setText(_translate("QQemail", "添加附件"))
            self.label_5.setText(_translate("QQemail", "邮件轰炸"))
            self.send_button_2.setText(_translate("QQemail", "取消"))
            self.menu.setTitle(_translate("QQemail", "我的邮箱"))
    
    
    # main.py
    
    import sys
    import demo
    
    from PyQt5.QtWidgets import QApplication, QMainWindow
    from email.mime.multipart import MIMEMultipart
    from email.header import Header
    from email.mime.text import MIMEText
    from email import encoders
    import smtplib
    from email.mime.base import MIMEBase
    from email.utils import formataddr, parseaddr
    
    
    def _format_addr(s):
        name, addr = parseaddr(s)
        return formataddr((Header(name, "utf-8").encode(), addr))
    
    
    def send():
        # print(ui.lineEdit.text())
        # print(ui.lineEdit_2.text())
        # print(ui.lineEdit_3.text())
        # print(ui.lineEdit_4.text())
        # print(ui.spinBox.value())
        # print(ui.plainTextEdit.document().toPlainText())
        # print(ui.dateEdit.date().year())
        # print(ui.dateEdit.date().month())
        # print(ui.dateEdit.date().day())
        # print(ui.timeEdit.time().hour())
        # print(ui.timeEdit.time().minute())
    
        N = ui.spinBox.value()  # 轰炸次数
    
        # 使用SMTP发送邮件
        from_addr = ui.lineEdit.text()
        from_pwd = ui.lineEdit_2.text()
        to_addr = ui.lineEdit_3.text()
        smtp_server = "smtp.qq.com"
    
        # 邮件对象:
        msg = MIMEMultipart()
        msg['From'] = _format_addr(u'(可编辑)<%s>' % from_addr)
        msg["To"] = _format_addr(u'(可编辑)<%s>' % to_addr)
        msg["Subject"] = Header(u'(可编辑)', 'utf-8').encode()
    
        # 邮件正文是MIMEText(二选一)
        content = ui.plainTextEdit.document().toPlainText()
        msg.attach(MIMEText(content, "plain", "utf-8"))
        # msg.attach(MIMEText('<html><body><h1>Hello</h1>' +
        #                     '<p><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1113%2F0F220092919%2F200F2092919-10-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1656580349&t=2592c4a48c4fdcf31ab501b9f534e516"></p>' +
        #                     '</body></html>', 'html', 'utf-8'))
    
        # 添加附件就是加上一个MIMEBase,从本地读取一个图片:
        with open('./' + ui.lineEdit_4.text(), "rb") as fp:
            mime = MIMEBase("image", "jpg", filename=ui.lineEdit_4.text())
            mime.add_header('Content-Disposition', 'attachment', filename=ui.lineEdit_4.text())
            mime.add_header('Content-ID', '<0>')
            mime.add_header('X-Attachment-Id', '0')
            mime.set_payload(fp.read())
            encoders.encode_base64(mime)
            msg.attach(mime)
    
        server = smtplib.SMTP_SSL(smtp_server, 465)
        server.set_debuglevel(1)
        server.login(from_addr, from_pwd)
        while N:
            server.sendmail(from_addr, [to_addr], msg.as_string())
            N -= 1
        # 停止服务
        server.quit()
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        MainWindow = QMainWindow()
        ui = demo.Ui_QQemail()
        ui.setupUi(MainWindow)
        MainWindow.show()
        ui.send_button.clicked.connect(send)
        sys.exit(app.exec_())
    
    
    

    最终效果图:




    https://wwb.lanzouh.com/iRaYw05q1spg

    相关文章

      网友评论

        本文标题:NJUPT《网络编程》

        本文链接:https://www.haomeiwen.com/subject/bqkxmrtx.html