import tkinter
import smtplib
from email.mime.text import MIMEText
import time
class Sendmail():
def __init__(self):
window=tkinter.Tk()#创建窗口对象
window.geometry("400x500")#定义窗口尺寸本例定义大小
window.resizable(width=False,height=False)#3,定义窗口拉伸权限,默认为可拉伸,此时将其设定为不可拉伸
window.title("邮件发送")#增加标题
lable=tkinter.Label(window,text="邮箱主题")#在窗口中添加文本标签 label
lable.pack()#锁定
self.title_input=tkinter.Entry(window,width="50")#在窗口中添加一个文本框
self.title_input.pack()
lable = tkinter.Label(window, text="邮箱内容")
lable.pack()
self.content_input = tkinter.Entry(window, width="50")
self.content_input.pack()
lable=tkinter.Label(window,text="发件人邮箱")
lable.pack()
self.user_input = tkinter.Entry(window, width="50")
self.user_input.pack()
lable=tkinter.Label(window,text="发件人授权码")
lable.pack()
self.pwd_input = tkinter.Entry(window, width="50")
self.pwd_input.pack()
lable = tkinter.Label(window, text="收件人邮箱")
lable.pack()
self.to_input = tkinter.Entry(window, width="50")
self.to_input.pack()
#按钮
btn=tkinter.Button(window,text="发送",command=self.send)
btn.pack()
window.mainloop()
def send(self):
#打印输入框信息
#发件人
user=self.user_input.get()
#收件人
to=self.to_input.get()
#密码
pwd=self.pwd_input.get()
#邮箱内容
content=self.content_input.get()
#邮箱主题
title=self.title_input.get()
#定义邮箱服务
server="smtp.qq.com"
msg=MIMEText(content)
msg["subject"]=title
msg["From"]=user
send=smtplib.SMTP(server,25)
send.login(user=user,password=pwd)
send.sendmail(from_addr=user, to_addrs=to, msg=msg.as_string())
if __name__=='__main__':
sendmail=Sendmail()
2.不使用类:
#邮箱发送,tk gui 窗口搭建,面型对象思维
import tkinter
import smtplib
from email.mime.text import MIMEText
import time
window=tkinter.Tk()#创建窗口对象
window.geometry("400x500")#定义窗口尺寸本例定义大小
window.resizable(width=False,height=False)#3,定义窗口拉伸权限,默认为可拉伸,此时将其设定为不可拉伸
window.title("邮件发送")#增加标题
lable=tkinter.Label(window,text="邮箱主题")#在窗口中添加文本标签 label
lable.pack()#锁定
title_input=tkinter.Entry(window,width="50")#在窗口中添加一个文本框
title_input.pack()
lable = tkinter.Label(window, text="邮箱内容")
lable.pack()
content_input = tkinter.Entry(window, width="50")
content_input.pack()
lable=tkinter.Label(window,text="发件人邮箱")
lable.pack()
user_input = tkinter.Entry(window, width="50")
user_input.pack()
lable=tkinter.Label(window,text="发件人授权码")
lable.pack()
pwd_input = tkinter.Entry(window, width="50")
pwd_input.pack()
lable = tkinter.Label(window, text="收件人邮箱")
lable.pack()
to_input = tkinter.Entry(window, width="50")
to_input.pack()
def send():
# 打印输入框信息
# 发件人
user = user_input.get()
# 收件人
to = to_input.get()
# 密码
pwd = pwd_input.get()
# 邮箱内容
content = content_input.get()
# 邮箱主题
title = title_input.get()
# 定义邮箱服务
server = "smtp.qq.com"
msg = MIMEText(content)
msg["subject"] = title
msg["From"] = user
send = smtplib.SMTP(server, 25)
send.login(user=user, password=pwd)
send.sendmail(from_addr=user, to_addrs=to, msg=msg.as_string())
#按钮
btn=tkinter.Button(window,text="发送",command=send)
btn.pack()
window.mainloop()
运行结果如下: