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 条评论
登录 后参与评论

相关文章

来自专栏C#

DotNet加密方式解析--非对称加密

    新年新气象,也希望新年可以挣大钱。不管今年年底会不会跟去年一样,满怀抱负却又壮志未酬。(不过没事,我已为各位卜上一卦,卦象显示各位都能挣钱...)...

4828
来自专栏魂祭心

原 canvas绘制clock

4034
来自专栏java 成神之路

使用 NIO 实现 echo 服务器

4537
来自专栏杨龙飞前端

scrollto 到指定位置

2494
来自专栏pangguoming

Spring Boot集成JasperReports生成PDF文档

由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲...

1.2K7
来自专栏转载gongluck的CSDN博客

cocos2dx 打灰机

#include "GamePlane.h" #include "PlaneSprite.h" #include "BulletNode.h" #include...

5346
来自专栏一个会写诗的程序员的博客

Spring Reactor 项目核心库Reactor Core

Non-Blocking Reactive Streams Foundation for the JVM both implementing a Reactiv...

2132
来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显,操作也非常简便,强烈推荐各位在集群上线之前进行这一操作,能够极大的提升整个集群...

3095
来自专栏我和未来有约会

Silverlight第三方控件专题

这里我收集整理了目前网上silverlight第三方控件的专题,若果有所遗漏请告知我一下。 名称 简介 截图 telerik 商 RadC...

3985
来自专栏跟着阿笨一起玩NET

c#实现打印功能

2632

扫码关注云+社区