# -*- 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. Vue指令 常见的几个内置指令

    1.v-if指令:判断指令,根据表达式值得真假来插入或删除相应的值. 2.v-show指令:条件渲染指令,无论返回的布尔值是true还是false,元素都会存在在html中,只是false的元素会隐藏 ...

  2. sql-case when 条件1 then 取值1 when 条件2 then 取值2 else 取值3 end

    遇到 XXX情况 就 XXX 遇不到就 XXX 结束case when …… then …… else …… end 例如一个3条件取值的字段: case when 条件1 then 取值1 when ...

  3. 基于Python在MacOS上安装robotframework-ride

    基于Python在MacOS上安装robotframework-ride https://www.jb51.net/article/153665.htm https://www.jianshu.com ...

  4. javascript 异步操作,串形执行,并行执行

    单线程模型 单线程模型指的是,JavaScript 只在一个线程上运行.也就是说,JavaScript 同时只能执行一个任务,其他任务都必须在后面排队等待. 注意,JavaScript 只在一个线程上 ...

  5. luogu11月月赛T3咕咕咕(组合数学)

    题目描述 小 F 是一个能鸽善鹉的同学,他经常把事情拖到最后一天才去做,导致他的某些日子总是非常匆忙. 比如,时间回溯到了 2018 年 11 月 3 日.小 F 望着自己的任务清单: 看 iG 夺冠 ...

  6. 01.centos7环境准备

    博客为日常工作学习积累总结: 1.环境准备: 系统版本:CentOS-7-x86_64-Minimal-1810.iso 运行环境:虚拟机windows上的VM 15 系统安装:参照老男孩运维要求 2 ...

  7. Java 遍历方法总结

    package com.zlh; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; im ...

  8. svg路径动画心得

    svg动画,随着路线运动,项目中需要用到,接触的时候感觉很高级,但是不会-无从下手呀!于是在网上找相关资料,先借鉴再修改成自己的. <svg width="500" heig ...

  9. 宏观看restframework序列化

    序列化 序列化意义 web有两种应用模式,一种是前后端不分离,一种是前后端分离,当前后端分离的时候,后端只需要向前端传输数据即可,不需要进行其他的操作,一般如果是中大型公司,都是前后端分离,这也是目前 ...

  10. jquery ajax 滚动加载数据

    jquery php 滚动加载数据(文件包 rollingpage) 效果如下: 页面加载时候($function(){ 自动加载第一页数据 }) 设置: var winH = $(window).h ...