python QQ邮件发送+可视化(SMTP)

解释在代码的注释中


'''
	邮件发送并窗口化
'''

import tkinter
from email.mime.text import MIMEText
import smtplib

class sendMail:
	# 默认执行
	def __init__(self):
		windows = tkinter.Tk()
		# 设置标题
		windows.title("邮件发送")
		# 窗口大小
		windows.geometry("500x400")
		# 窗口大小不可变
		windows.resizable(width=False, height=False)

		# 标题
		self.text_title = tkinter.Label(windows, text="邮件主题")
		# 显示
		self.text_title.pack()
		# 输入框
		self.ed_sendTitle = tkinter.Entry(windows, width="50")
		self.ed_sendTitle.pack()

		self.text_text = tkinter.Label(windows, text="邮件正文")
		self.text_text.pack()
		self.ed_sendText = tkinter.Entry(windows, width="50")
		self.ed_sendText.pack()

		self.text_username = tkinter.Label(windows, text="发件人昵称")
		self.text_username.pack()
		self.ed_sendUsername = tkinter.Entry(windows, width="50")
		self.ed_sendUsername.pack()

		self.text_name = tkinter.Label(windows, text="收件人邮箱")
		self.text_name.pack()
		self.ed_sendName = tkinter.Entry(windows, width="50")
		self.ed_sendName.pack()

		# 发送按钮,同时调用sendMsg方法
		btn = tkinter.Button(windows, text="发送", command=self.sendMsg)
		btn.pack()

		# 显示窗口
		windows.mainloop()
	def sendMsg(self):
		title = self.ed_sendTitle.get()
		text = self.ed_sendText.get()
		sendName = self.ed_sendUsername.get()
		toUserName = self.ed_sendName.get()
		# 默认发送人,
		sendUserName = "[email protected]"
		# 在qq邮箱里开通SMTP服务得到的验证码
		sendCode = "goccesbxlniibhef"
		
		# 发送的内容
		msg = MIMEText(text)
		# 标题
		msg["subject"] = title
		# 发送人昵称
		msg["From"] = sendName

		# 发送人信息的登录
		email = smtplib.SMTP("smtp.qq.com",25)
		email.login(sendUserName,sendCode)
		
		# 从发送人发送到收件人
		email.sendmail(sendUserName,toUserName,msg=msg.as_string())
		
		# 退出邮箱
		email.quit()
		
if __name__ == '__main__':
	# 类的调用
	sendMail()

你可能感兴趣的:(python)