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

# Form implementation generated from reading ui file 'SMTP.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost! import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow,QFileDialog import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindow,self).__init__()
self.setupUi(self)
self.retranslateUi(self) def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1095, 842)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label_title = QtWidgets.QLabel(self.centralwidget)
self.label_title.setGeometry(QtCore.QRect(310, 300, 81, 18))
self.label_title.setObjectName("label_title")
self.textEdit_title = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit_title.setGeometry(QtCore.QRect(410, 290, 351, 41))
self.textEdit_title.setObjectName("textEdit_title")
self.label_passage = QtWidgets.QLabel(self.centralwidget)
self.label_passage.setGeometry(QtCore.QRect(60, 350, 81, 18))
self.label_passage.setObjectName("label_passage")
self.textEdit_passage = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit_passage.setGeometry(QtCore.QRect(60, 380, 971, 361))
self.textEdit_passage.setObjectName("textEdit_passage")
self.button_send = QtWidgets.QPushButton(self.centralwidget)
self.button_send.setGeometry(QtCore.QRect(510, 750, 112, 34))
self.button_send.setObjectName("button_send")
self.button_send.clicked.connect(self.send_email)
self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(60, 40, 271, 81))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.textEdit_smtpserver = QtWidgets.QTextEdit(self.gridLayoutWidget)
self.textEdit_smtpserver.setObjectName("textEdit_smtpserver")
self.gridLayout.addWidget(self.textEdit_smtpserver, 0, 2, 1, 1)
self.textEdit_port = QtWidgets.QTextEdit(self.gridLayoutWidget)
self.textEdit_port.setObjectName("textEdit_port")
self.gridLayout.addWidget(self.textEdit_port, 1, 2, 1, 1)
self.label_port = QtWidgets.QLabel(self.gridLayoutWidget)
self.label_port.setObjectName("label_port")
self.gridLayout.addWidget(self.label_port, 1, 0, 1, 1)
self.label_smtpserver = QtWidgets.QLabel(self.gridLayoutWidget)
self.label_smtpserver.setObjectName("label_smtpserver")
self.gridLayout.addWidget(self.label_smtpserver, 0, 0, 1, 1)
self.gridLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget_2.setGeometry(QtCore.QRect(60, 150, 531, 121))
self.gridLayoutWidget_2.setObjectName("gridLayoutWidget_2")
self.gridLayout_2 = QtWidgets.QGridLayout(self.gridLayoutWidget_2)
self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
self.gridLayout_2.setObjectName("gridLayout_2")
self.label_receivernickname = QtWidgets.QLabel(self.gridLayoutWidget_2)
self.label_receivernickname.setObjectName("label_receivernickname")
self.gridLayout_2.addWidget(self.label_receivernickname, 2, 0, 1, 1)
self.label_sender = QtWidgets.QLabel(self.gridLayoutWidget_2)
self.label_sender.setObjectName("label_sender")
self.gridLayout_2.addWidget(self.label_sender, 0, 0, 1, 1)
self.label_receiver = QtWidgets.QLabel(self.gridLayoutWidget_2)
self.label_receiver.setObjectName("label_receiver")
self.gridLayout_2.addWidget(self.label_receiver, 1, 0, 1, 1)
self.textEdit_receiver = QtWidgets.QTextEdit(self.gridLayoutWidget_2)
self.textEdit_receiver.setObjectName("textEdit_receiver")
self.gridLayout_2.addWidget(self.textEdit_receiver, 1, 2, 1, 1)
self.textEdit_sender = QtWidgets.QTextEdit(self.gridLayoutWidget_2)
self.textEdit_sender.setObjectName("textEdit_sender")
self.gridLayout_2.addWidget(self.textEdit_sender, 0, 2, 1, 1)
self.textEdit_receivernickname = QtWidgets.QTextEdit(self.gridLayoutWidget_2)
self.textEdit_receivernickname.setObjectName("textEdit_receivernickname")
self.gridLayout_2.addWidget(self.textEdit_receivernickname, 2, 2, 1, 1)
self.gridLayoutWidget_3 = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget_3.setGeometry(QtCore.QRect(500, 40, 531, 81))
self.gridLayoutWidget_3.setObjectName("gridLayoutWidget_3")
self.gridLayout_3 = QtWidgets.QGridLayout(self.gridLayoutWidget_3)
self.gridLayout_3.setContentsMargins(0, 0, 0, 0)
self.gridLayout_3.setObjectName("gridLayout_3")
self.smtp_username = QtWidgets.QLabel(self.gridLayoutWidget_3)
self.smtp_username.setObjectName("smtp_username")
self.gridLayout_3.addWidget(self.smtp_username, 0, 0, 1, 1)
self.textEdit_username = QtWidgets.QTextEdit(self.gridLayoutWidget_3)
self.textEdit_username.setObjectName("textEdit_username")
self.gridLayout_3.addWidget(self.textEdit_username, 0, 1, 1, 1)
self.textEdit_pwd = QtWidgets.QTextEdit(self.gridLayoutWidget_3)
self.textEdit_pwd.setObjectName("textEdit_pwd")
self.gridLayout_3.addWidget(self.textEdit_pwd, 1, 1, 1, 1)
self.label_pwd = QtWidgets.QLabel(self.gridLayoutWidget_3)
self.label_pwd.setObjectName("label_pwd")
self.gridLayout_3.addWidget(self.label_pwd, 1, 0, 1, 1)
self.button_upload = QtWidgets.QPushButton(self.centralwidget)
self.button_upload.setGeometry(QtCore.QRect(640, 180, 51, 61))
self.button_upload.setObjectName("button_upload")
self.button_upload.clicked.connect(self.upload_file) # self.listView = QtWidgets.QListView(self.centralwidget)
# self.listView.setGeometry(QtCore.QRect(700, 168, 331, 101))
# self.listView.setObjectName("listView")
self.file_list = QtWidgets.QTextEdit(self.centralwidget)
self.file_list.setEnabled(False)
self.file_list.setGeometry(QtCore.QRect(700, 180, 331, 101))
self.file_list.setObjectName("file_list") self.checkBox_SSL = QtWidgets.QCheckBox(self.centralwidget)
self.checkBox_SSL.setGeometry(QtCore.QRect(360, 70, 105, 22))
self.checkBox_SSL.setObjectName("checkBox_SSL") self.label_list = QtWidgets.QLabel(self.centralwidget)
self.label_list.setGeometry(QtCore.QRect(700, 150, 81, 18))
self.label_list.setObjectName("label_list")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1095, 30))
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", "SMTP邮件发送"))
self.label_title.setText(_translate("MainWindow", "标题"))
self.label_passage.setText(_translate("MainWindow", "正文"))
self.button_send.setText(_translate("MainWindow", "发送"))
self.label_port.setText(_translate("MainWindow", "端口号"))
self.label_smtpserver.setText(_translate("MainWindow", "smtp服务器"))
self.label_sender.setText(_translate("MainWindow", "发件人"))
self.label_receiver.setText(_translate("MainWindow", "收件人"))
self.smtp_username.setText(_translate("MainWindow", "用户名"))
self.label_pwd.setText(_translate("MainWindow", "授权码/密码"))
self.button_upload.setText(_translate("MainWindow", "上传\n"
"文件"))
self.label_list.setText(_translate("MainWindow", "附件列表"))
self.label_receivernickname.setText(_translate("MainWindow", "收件人昵称"))
self.checkBox_SSL.setText(_translate("MainWindow", "使用SSL")) message = MIMEMultipart()
def send_email(self):
my_sender = self.textEdit_username.toPlainText() # 发件人邮箱账号
my_pass = self.textEdit_pwd.toPlainText() # 发件人邮箱密码
my_user = self.textEdit_receiver.toPlainText() # 收件人邮箱账号,我这边发送给自己
sender_kickname = self.textEdit_sender.toPlainText() # 发件人邮箱
title = self.textEdit_title.toPlainText()
passage = self.textEdit_passage.toPlainText()
SMTP_server = self.textEdit_smtpserver.toPlainText()
port = self.textEdit_port.toPlainText()
ret = True
try: self.message['From'] = formataddr([sender_kickname, my_sender]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号
self.message['To'] = formataddr([self.textEdit_receivernickname.toPlainText(), my_user]) # 括号里的对应收件人邮箱昵称、收件人邮箱账号
self.message['Subject'] = title # 邮件的主题,也可以说是标题 self.message.attach(MIMEText(passage, 'plain', 'utf-8'))
if(self.checkBox_SSL.checkState()==0):
server = smtplib.SMTP(SMTP_server, port)
else:
server = smtplib.SMTP_SSL(SMTP_server, port) # 发件人邮箱中的SMTP服务器,端口是25
server.login(my_sender, my_pass) # 括号中对应的是发件人邮箱账号、邮箱密码
server.sendmail(my_sender, [my_user, ], self.message.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
server.quit() # 关闭连接
except Exception: # 如果 try 中的语句没有执行,则会执行下面的 ret=False
ret = False
if ret:
print("邮件发送成功")
self.message = MIMEMultipart() #清空邮件内容
else:
print("邮件发送失败") def upload_file(self):
openfile_name = QFileDialog.getOpenFileName(self, '选择文件', '', 'All Types(*.*)')
if(('', '')!=openfile_name):
import os
attach_file = MIMEText(open(openfile_name[0], 'rb').read(), 'base64', 'utf-8')
attach_file["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
filename=os.path.basename(openfile_name[0])
attach_file["Content-Disposition"] = 'attachment; filename=\"'+filename+'\"'
self.message.attach(attach_file)
# self.listView.addAction(filename)
files=self.file_list.toPlainText()
self.file_list.setText(files+"\n"+filename) if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

  或者在https://paste.ubuntu.com/p/JrJmpktw2s/看也是一样的

pyqt5实现SMTP邮件发送的更多相关文章

  1. python学习笔记(SMTP邮件发送:带附件)

    博主有段时间没有更新博客了 先整理一个之前整理过的SMTP邮件发送,这次是带附件的功能 #!/usr/bin/env python # -*- coding: utf_8 -*- from email ...

  2. python学习笔记(SMTP邮件发送)

    想着给框架添加邮件发送功能.所以整理下python下邮件发送功能 首先python是支持邮件的发送.内置smtp库.支持发送纯文本.HTML及添加附件的邮件 之后是邮箱.像163.qq.新浪等邮箱默认 ...

  3. python SMTP邮件发送(转载)

    Python SMTP发送邮件 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. py ...

  4. C# SMTP邮件发送 分类: C# 2014-07-13 19:10 334人阅读 评论(1) 收藏

    邮件发送在网站应用程序中经常会用到,包括您现在看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,做了一个客户端Demo,希望对有需要的童鞋有所帮助: 核心代码: ...

  5. C# SMTP邮件发送 分类: C# 2014-07-13 19:10 333人阅读 评论(1) 收藏

    邮件发送在网站应用程序中经常会用到,包括您现在看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,做了一个客户端Demo,希望对有需要的童鞋有所帮助: 核心代码: ...

  6. java实现smtp邮件发送

    一.准备工作 首先你需要已一个发送邮箱,一般的邮箱都有SMTP.POP3服务,比如QQ邮箱,登陆QQ邮箱开启SMTP服务,开启是服务器会提示你设置独立密码,这个密码是跟邮箱正常登陆的密码不同的,这个是 ...

  7. Python SMTP邮件发送

    SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块: email负责构造邮件 ...

  8. C# SMTP 邮件发送之QQ邮箱篇

    邮件发送大家都已经非常熟悉了,微软自带的System.Net.Mail也很好用,那为什么还要说呢? QQ邮箱的SMTP以前是非SSL,用未加密的25端口,后来发送都改成SSL了,端口为465或587( ...

  9. JavaUtil smtp 邮件发送

    需要用到的jar包:javax.mail.jar package com.lee.util; import java.io.UnsupportedEncodingException; import j ...

随机推荐

  1. Notes 20180311 : String第三讲_深入了解String

    很多前辈我可能对于我的这节文章很困惑,觉得String这个东西还有什么需要特别了解的吗?其实不然,String是一个使用十分频繁的工具类,不可避免地我们也会遇到一些陷阱,深入了解String对于我们避 ...

  2. 【腾讯敏捷转型No.5】需求没做完可以发布嘛

    很多人对于敏捷的第一直觉就是“快”,开发快,测试快,发布快,并不知道如何把这个“快”应用到敏捷实践中,下面我们来分析一下导致工作效率低的核心原因.没有使用敏捷之前,在大多数情况下,项目管理都需要开各种 ...

  3. 怎么在苹果Mac虚拟机上安装Win7

    怎么在苹果Mac虚拟机上安装Win7 使用 Mac 系统的用户,因为一些软件或是应用的原因,可能需要 Windows 系统才能完成.那如果不想在自己的 Mac 电脑上安装双启动系统的话,我们还可以在 ...

  4. java环境变量配置(win7)

    JDK1.8 1.单击“计算机-属性-高级系统设置”,单击“环境变量”.在“系统变量”栏下单击“新建”,创建新的系统环境变量. 2.  (1)新建->变量名"JAVA_HOME&quo ...

  5. POJ2311 Cutting Game(博弈论)

    总时间限制: 1000ms 内存限制: 65536kB 描述 Urej loves to play various types of dull games. He usually asks other ...

  6. 001_02-python基础习题答案

    python 基础习题 执行 Python 脚本的两种方式 如:脚本/python/test.py 第一种方式:python /python/test.py 第二中方式:在test.py中声明:/us ...

  7. python3使用bencode库实现BT种子生成磁力链接

    python3 需要使用 pip install py3-bencode安装py3-bencode库. pip install py3-bencode 这里使用当前目录下的 1.torrent 文件转 ...

  8. C语言不定型参数函数定义

    我们在C语言中定义一个函数,通常都是需要在函数原型中规定这个函数需要提供什么类型的参数以及需要提供多少个.也就是,你的参数必须明确.但是我们调用函数库中的printf和scanf函数会发现,它们似乎是 ...

  9. 基于R实现k-means法与k-medoids法

    k-means法与k-medoids法都是基于距离判别的聚类算法.本文将使用iris数据集,在R语言中实现k-means算法与k-medoids算法. k-means聚类 首先删去iris中的Spec ...

  10. USB-Blaster驱动安装失败——文件哈希值不在指定目录中

    右击此电脑,选择管理,选择设备管理器,更新USB-Blaster驱动出现问题 问题: 文件的哈希值不在指定的目录文件中,如图: 解决办法: Windows键+R→shutdown.exe /r /o ...