python收发邮件客户端

基于pyqt5 和 smtplib pop3标准邮箱协议开发邮件客户端

发送 SMTP

#!/usr/bin/env python
#-*- coding:utf-8 -*- 
#@Time: 2018/5/2上午11:27
#@Author:zhangrongwu
#@File:qt_email.py


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication

import sys

import smtplib
from email.mime.text import MIMEText
from email.header import  Header


class Email_MaiWindow(object):
    def setupUI(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")#相当于iOS contentview

        self.label_Host = QtWidgets.QLabel(self.centralwidget)
        self.label_Host.setGeometry(QtCore.QRect(30, 10, 200, 20))
        self.label_Host.setObjectName("label_Host")
        self.label_Host.setText("webmail.xinaogroup.com")#每个公司地址不一样


        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)#相当于iOS addsubview 到父控件
        self.lineEdit.setGeometry(QtCore.QRect(30, 50, 200, 30))#相当于iOS self.lineEdit.frame = ...
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit.setPlaceholderText("输入你的邮箱")#站位字符串


        self.lineEdit_Pwd = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_Pwd.setGeometry(QtCore.QRect(30, 90, 200, 30))
        self.lineEdit_Pwd.setObjectName("lineEdit_Pwd")
        self.lineEdit_Pwd.setPlaceholderText("输入邮箱密码")

        self.lineEdit_To = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_To.setGeometry(QtCore.QRect(260, 50, 200, 30))
        self.lineEdit_To.setObjectName("lineEdit_To")
        self.lineEdit_To.setPlaceholderText("收件人邮箱")

        self.lineEdit_Subject = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_Subject.setGeometry(QtCore.QRect(260, 90, 200, 30))
        self.lineEdit_Subject.setObjectName("lineEdit_Subject")
        self.lineEdit_Subject.setPlaceholderText("主题")


        self.contentMsg = QtWidgets.QTextEdit(self.centralwidget)#多行文本框,相当于iOS UITextview
        self.contentMsg.setGeometry(QtCore.QRect(30, 130, 400, 100))
        self.contentMsg.setObjectName("contentMsg")
        self.contentMsg.setPlaceholderText("填写发送的内容")



        self.sendButton = QtWidgets.QPushButton(self.centralwidget)
        self.sendButton.setGeometry(QtCore.QRect(30,250, 80, 50))
        self.sendButton.setObjectName("sendButton")
        self.sendButton.setStyleSheet("font:20pt \"Agency FB\";")
        self.sendButton.setText("发送")
        self.sendButton.clicked.connect(lambda : self.send())




        MainWindow.setCentralWidget(self.centralwidget)#把父控件添加到窗口上

    def send(self):
        sender = self.lineEdit.text()
        receiver = self.lineEdit_To.text()
        content = self.contentMsg.toPlainText()
        passWord = self.lineEdit_Pwd.text()
        message = MIMEText(content, 'plain', 'utf-8')
        message["From"] = Header(sender, 'utf-8')
        message["To"] = Header(receiver, 'utf-8')

        subject = self.lineEdit_Subject.text()
        message['Subject'] = Header(subject, 'utf-8')

        mailhost = self.label_Host.text()




        # 构造附件1,传送当前目录下的 test.txt 文件
        # att1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8')
        # att1["Content-Type"] = 'application/octet-stream'
        # # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
        # att1["Content-Disposition"] = 'attachment; filename="test.txt"'
        # message.attach(att1)
        #
        # # 构造附件2,传送当前目录下的 runoob.txt 文件
        # att2 = MIMEText(open('runoob.txt', 'rb').read(), 'base64', 'utf-8')
        # att2["Content-Type"] = 'application/octet-stream'
        # att2["Content-Disposition"] = 'attachment; filename="runoob.txt"'
        # message.attach(att2)

        try:
            # smtpObj = smtplib.SMTP(localhost) #如果 SMTP 在你的本机上,你只需要指定服务器地址为 localhost 即可,无需密码登陆
            smtpObj = smtplib.SMTP(mailhost, port=25)#25为 SMTP 默认端口号
            smtpObj.set_debuglevel(1)
            smtpObj.login(sender, passWord)
            smtpObj.sendmail(sender, [receiver], message.as_string())
            smtpObj.quit()
            print("邮件发送成功")
        except smtplib.SMTPException as msg:
            print(msg)
            print("邮件无法发送")




if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()#创建窗口
    ui = Email_MaiWindow()#初始化Email_MaiWindow类
    ui.setupUI(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

接收 POP3

#!/usr/bin/env python
#-*- coding:utf-8 -*- 
#@Time: 2018/5/10下午5:33
#@Author:zhangrongwu
#@File:qt_email_pop3.py

import poplib

email = input('Email:' )
password = input('Password:' )
pop3_server = input('POP3 server:' )

server = poplib.POP3(pop3_server)

print(server.getwelcome())
server.user(email)
server.pass_(password)
print('message: %s.size: %s' % server.stat())

resp, mails, octets = server.list()

print(mails)

index = len(mails)

resp, lines, octets = server.retr(index)

msg_content = '\r\n'.join(lines)

# msg = Parser().parsestr(msg_content)

server.quit()

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏前端吧啦吧啦

手把手教你全家桶之React(二)

923
来自专栏向治洪

React Native和Android整合详解

前言 按照React Native的迭代速度,使用官网的文档,已经不能很顺利的实现React Native和Android的有效整合。React Native最...

2375
来自专栏Golang语言社区

用 Go 写一个轻量级的 ssh 批量操作工具

大家都知道 Ansible 是功能超级强大的自动化运维工具,十分的高大上。太高大上了以至于在低端运维有点水土不服,在于三点:

1892
来自专栏杨建荣的学习笔记

恢复控制文件避免使用resetlogs选项 (r10笔记第12天)

在搭建Data Guard的时候,我们可以直接从主库生成一个备库控制文件,或者拷贝一个备库的控制文件即可,后续的工作就交给Data Guard来自动恢复...

3024
来自专栏F_Alex

SpringCloud-微服务网关ZUUL(六)

1015
来自专栏杂七杂八

sublime解决There are no packages available for installation及markdown插件

There are no packages available for installation sublime是一个轻便优秀的代码编写工具,而且其众多插件也为...

2928
来自专栏前端吧啦吧啦

手把手教你全家桶之React(二)

4068
来自专栏技术之路

visual studio 2012 的制作ActiveX、打包和发布

开发环境是Vs 2012  Framework 4.0 源码和制作工具在文章最下边 一. ActiveX控件Demo 新建一个Window窗体控件库项目 ? 在...

1759
来自专栏蘑菇先生的技术笔记

Go中原始套接字的深度实践

原始套接字(raw socket)是一种网络套接字,允许直接发送/接收更底层的数据包而不需要任何传输层协议格式。平常我们使用较多的套接字(socket)都是基于...

822
来自专栏蜉蝣禅修之道

ns-3构建简单点对点网络

2445

扫码关注云+社区