# -*- 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. Fpm启动机制及流程分析———详细

    FPM(FastCGI Process Manager)是PHP FastCGI运行模式的一个进程管理器,从它的定义可以看出,FPM的核心功能是进程管理,那么它用来管理什么进程呢?这个问题就需要从Fa ...

  2. Windows Redis 取消保护模式C#进行访问

    运行redis服务程序和客户端程序设置即可. config set protected-mode “no”

  3. SpringAOP的自定义注解实践

    springaop属于spring的重要属性,在java中有相当广泛的用途,大家一般都接触过aop实现事务的管理,在xml里配好声明式事务,然后直接在service上直接加上相应注解即可, 今天我们来 ...

  4. Redis的特性以及优势(附官网)

    NoSQL:一类新出现的数据库(not only sql) 泛指非关系型的数据库 不支持SQL语法 存储结构跟传统关系型数据库中的那种关系表完全不同,nosql中存储的数据都是KV形式 NoSQL的世 ...

  5. php 获取当前完整url地址

    echo $url = $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["SERVER_NAME"].$_SERVER[&q ...

  6. 大数据时代数据库-云HBase架构&生态&实践

    业务的挑战 存储量量/并发计算增大 现如今大量的中小型公司并没有大规模的数据,如果一家公司的数据量超过100T,且能通过数据产生新的价值,基本可以说是大数据公司了 .起初,一个创业公司的基本思路就是首 ...

  7. 跟着马哥学python-day02

    1. 运算符 计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算数运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算. 1.1 算数运算 以下假设变量:a=10,b= ...

  8. A1037

    给两个序列,一一对应相乘,求最大和. 0不算数,输入时按正负共分为4个数组. #include<cstdio> #include<algorithm> #include< ...

  9. PTA基础编程题目集6-4求自定类型元素的平均 (函数题)

    6-4 求自定类型元素的平均 (10 分)  本题要求实现一个函数,求N个集合元素S[]的平均值,其中集合元素的类型为自定义的ElementType. 函数接口定义: ElementType Aver ...

  10. XAMPP之Mysql启动失败

    启动XAMPP中的Mysql出现如下: 可能的原因是本地有多个MySQL,所以要在注册表编辑器中将imagePath改成XAMPP中的mysql的地址.(打开注册表编辑器:win+R,输入regedi ...