补充函数的编写。 import smtplib from email.mime.text import MIMEText from email.header import Header from email.mime.multipart import MIMEMultipart #发送带有附件的邮件时引入 host="smtp.qq.com" port=587 smtp_obj=smtplib.SMTP('smtp.qq.com') def fun_login(): pass def fun_send(): pass def fun_logout(): pass root=tk.Tk() root.title('发送邮件') root.geometry('420x340') root.resizable(width=False,height=False) lbluser=tk.Label(root,text='用户名') lbluser.place(x=20,y=10,width=40,height=30) var_user=tk.StringVar(value='') edituser=tk.Entry(root,textvariable=var_user) edituser.place(x=10,y=50,width=150,height=30) lblcode=tk.Label(root,text='授权码') lblcode.place(x=170,y=10,width=40,height=30) var_code=tk.StringVar(value='') editcode=tk.Entry(root,textvariable=var_code) editcode.place(x=170,y=50,width=150,height=30) btnlogin=tk.Button(root,text='登录',command=fun_login) btnlogin.place(x=340,y=40,width=60,height=30) lblrecv=tk.Label(root,text='接受邮箱') lblrecv.place(x=10,y=110,width=50,height=30) var_recv=tk.StringVar(value='') editrecv=tk.Entry(root,textvariable=var_recv) editrecv.place(x=70,y=110,width=200,height=30) lbltext=tk.Label(root,text='邮件内容') lbltext.place(x=10,y=150,width=50,height=30) mailtext=tk.Text(root) mailtext.place(x=70,y=150,width=200,height=60) btnsend=tk.Button(root,text="发送邮件",command=fun_send) btnsend.place(x=30,y=220,width=80,height=30) btnlogout=tk.Button(root,text="退出",command=fun_logout) btnlogout.place(x=150,y=220,width=80,height=30) root.mainloop()
时间: 2023-08-06 12:14:26 浏览: 133
以下是完整代码,包括补充的三个函数fun_login、fun_send和fun_logout:
```python
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
# 发送带有附件的邮件时引入
from email.mime.application import MIMEApplication
host = "smtp.qq.com"
port = 587
smtp_obj = smtplib.SMTP(host, port)
def fun_login():
global smtp_obj
user = var_user.get()
code = var_code.get()
smtp_obj.login(user, code)
print('登录成功!')
def fun_send():
recv = var_recv.get()
text = mailtext.get('1.0', 'end')
msg = MIMEMultipart()
msg['From'] = Header(var_user.get(), 'utf-8')
msg['To'] = Header(recv, 'utf-8')
msg['Subject'] = Header('测试邮件', 'utf-8')
# 邮件正文
msg.attach(MIMEText(text, 'plain', 'utf-8'))
# 添加附件
filename = 'test.txt'
with open(filename, 'rb') as f:
attachment = MIMEApplication(f.read(), _subtype='txt')
attachment.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(attachment)
smtp_obj.sendmail(var_user.get(), recv, msg.as_string())
print('发送成功!')
def fun_logout():
global smtp_obj
smtp_obj.quit()
print('退出成功!')
root = tk.Tk()
root.title('发送邮件')
root.geometry('420x340')
root.resizable(width=False, height=False)
lbluser = tk.Label(root, text='用户名')
lbluser.place(x=20, y=10, width=40, height=30)
var_user = tk.StringVar(value='')
edituser = tk.Entry(root, textvariable=var_user)
edituser.place(x=10, y=50, width=150, height=30)
lblcode = tk.Label(root, text='授权码')
lblcode.place(x=170, y=10, width=40, height=30)
var_code = tk.StringVar(value='')
editcode = tk.Entry(root, textvariable=var_code)
editcode.place(x=170, y=50, width=150, height=30)
btnlogin = tk.Button(root, text='登录', command=fun_login)
btnlogin.place(x=340, y=40, width=60, height=30)
lblrecv = tk.Label(root, text='接受邮箱')
lblrecv.place(x=10, y=110, width=50, height=30)
var_recv = tk.StringVar(value='')
editrecv = tk.Entry(root, textvariable=var_recv)
editrecv.place(x=70, y=110, width=200, height=30)
lbltext = tk.Label(root, text='邮件内容')
lbltext.place(x=10, y=150, width=50, height=30)
mailtext = tk.Text(root)
mailtext.place(x=70, y=150, width=200, height=60)
btnsend = tk.Button(root, text="发送邮件", command=fun_send)
btnsend.place(x=30, y=220, width=80, height=30)
btnlogout = tk.Button(root, text="退出", command=fun_logout)
btnlogout.place(x=150, y=220, width=80, height=30)
root.mainloop()
```
注意,这个代码需要在安装了Tkinter和smtplib模块的Python环境中运行。其中,fun_login函数用于登录邮箱,fun_send函数用于发送邮件和附件,fun_logout函数用于退出邮箱。在发送邮件时,需要将邮件正文和附件都添加到MIMEMultipart对象中,然后发送邮件即可。
阅读全文