发送qq邮件客户端

此代码为所有qq邮箱用户都可用

import tkinter
import smtplib
from email.mime.text import MIMEText                                # 邮件文本
from email.mime.multipart import MIMEMultipart


class Sendemail:
    def __init__(self, sender, password, smtsever="smtp.qq.com",):
        self.smtsever = smtsever                                    # 服务器名称
        self.__sender = sender+"@qq.com"                            # 发送人者邮箱
        self.__password =password                                   # 发送者的SMTP授权码,具体获得可自行百度
        self.mailsever = smtplib.SMTP(self.smtsever, 25)            # 邮件服务器端口25
        self.mailsever.login(self.__sender, self.__password)        # 登录

    def send(self,filepath, maillist, message=None, title=None):    # 接受的参数有路径列表,收件人列表,内容和主题默认为空
        msg = MIMEMultipart()                                       # 创建MIMEMultipart的类
        for mail in maillist:                                       # 判断邮箱列表是否为空,如果是就加入自身账号
            if mail == '':
                maillist.append(self.__sender)
        if message != '':                                           # 如果message不为空就转化成邮件文本
            msg.attach(MIMEText(message))
        msg["Subject"] = title                                      # 将title设为邮件标题
        msg["From"] = self.__sender
        for path in filepath:                                       # 对于输入的所有附件路径进行遍历,如果为空就结束
            if path == '':
                continue
            else:
                att1 = MIMEText(open(path, 'rb').read(), "base64", "utf-8")     # 如果不为空就添加附件的
                att1["Content-Type"] = "application/octet-stream"
                filename = path[path.rfind("\\")+1:]                            # 因为输入的是绝对路径,所以返回最后一个\得出文件名称
                # att1["Content-Disposition"] = 'attachment ;filename = %s' % filename         #如果为全英文文件可以用这种方式
                att1.add_header('Content-Disposition', 'attachment', filename=('gbk', '', filename))    # 这种方式适合中英文文件
                msg.attach(att1)                                                # 每读取一个文件都加入到附件列表中
        self.mailsever.sendmail(self.__sender,                                  # 发送邮件,发邮件人
                                maillist,                                       # 邮件列表
                                msg.as_string())                                # 邮件的所有内容
        self.mailsever.quit()                                                   # 退出登录


class Inputview:
    '''定义一个登录界面窗口的类'''

    def __init__(self):
        self.win = tkinter.Tk()                                                 # 生成窗体
        self.win.title("qq邮箱发送客户端")                                       # 窗体的名称
        self.win.geometry("800x600+200+200")                                    # 窗体的大小

    def showview(self):                                                         # 展示窗体
        self.entry_place()                                                      # 运行文本输入函数
        self.button_OK()                                                        # 运行按钮函数
        self.labels()                                                           # 运行标签函数

        self.win.mainloop()                                                     # 窗口循环

    def entry_place(self):                                                      # 文本输入
        self.entry_email = tkinter.Entry(self.win, width=30)                    # 定义自身的邮箱输入窗口
        self.entry_email.place(x=300, y=200)                                    # 在x=300,y=200处放置
        self.entry_password = tkinter.Entry(self.win,width=30)                  # 定义一个授权码输入的窗口
        self.entry_password["show"] = "*"                                       # 设置展现出的密码为*
        self.entry_password.place(x=300, y=250)                                 # 在x =300,y=250处展示


    def button_OK(self):
        button_attchment_path = tkinter.Button(self.win, text="登录", command=self.email_list)    # 生成登录按钮
        button_attchment_path.place(x=390, y=275)                               # 登录按钮坐标显示

    def labels(self):
        email_label = tkinter.Label(self.win, text="输入你的qq号")              # 在邮箱输入上方放置一个标签提示输入内容
        email_label.place(x=300, y=175)                                         # 计算得到的位置
        password_label = tkinter.Label(self.win, text="输入你的smtp授权码")     # 在密码输入上方放置一个标签提示输入内容
        password_label.place(x=300, y=225)                                      # 计算得到的位置

    def email_list(self):
        email_email_account = self.entry_email.get()                            # 获取输入的邮箱账号
        email_password = self.entry_password.get()                              # 获取输入的授权码

        try:
            '''密码正确执行try之后的语句'''
            Sendemail(email_email_account, email_password)                      # 调用发邮件类的登录函数
            self.win.destroy()                                                  # 登录成功销毁登录界面窗口

            class Afterview:
                '''创建一个登录成功之后的窗口'''

                def __init__(self):
                    self.win1 = tkinter.Tk()                                    # 创建窗口
                    self.win1.title("发邮件")                                   # 窗口标题
                    self.win1.geometry("800x600+200+200")                       # 窗口大小和位置

                def showview(self):                                             # 展示窗口
                    self.labels()                                               # 生成标签
                    self.entrys()                                               # 生成输入框
                    self.button_send()                                          # 生成按钮

                    self.win1.mainloop()                                        # 窗口循环

                def labels(self):
                    label_title = tkinter.Label(self.win1, text="请输入邮件主题:")             # 标签函数,提示输入主题
                    label_title.place(x=100, y=100)                                             # 该标签的显示位置
                    label_message = tkinter.Label(self.win1, text="请输入邮件内容:")           # 提示输入邮件内容
                    label_message.place(x=100, y=200)                                           # 该标签的显示位置
                    label_attachment = tkinter.Label(self.win1, text="请输入邮件附件绝对路径(#号键分割多个附件地址):")
                    label_attachment.place(x=100, y=300)                                        # 提示输入附件位置,放置
                    label_emaillist=tkinter.Label(self.win1, text="请输入邮件附件绝对路径(#分割多个收件人):")
                    label_emaillist.place(x=100, y=400)                                         # 提示输入收件人邮箱,放置

                def entrys(self):                                                               # 标签函数
                    self.entry_title = tkinter.Entry(self.win1, width=200)                       # 生成一个全局的输入框,一边其他函数使用
                    self.entry_title.place(x=100, y=150)                                        # 标题输入框,放置
                    self.entry_message = tkinter.Entry(self.win1, width=200)                     # 生成内容输入框
                    self.entry_message.place(x=100, y=250)                                      # 放置的位置
                    self.entry_attachment = tkinter.Entry(self.win1, width=200)                  # 生成附件输入框
                    self.entry_attachment.place(x=100, y=350)                                   # 放置的位置
                    self.entry_email_list = tkinter.Entry(self.win1, width=200)                  # 生成收件人输入框
                    self.entry_email_list.place(x=100, y=450)                                   # 放置的位置

                def button_send(self):                                                          # 生成按钮函数
                    button_send = tkinter.Button(self.win1, text="发送", command=self.sendemail)  # 生成一个发送按钮,绑定发送函数
                    button_send.place(x=150, y=475)                                               # 放置

                def sendemail(self):                                                                # 发送邮件函数
                    title = self.entry_title.get()                                                  # 获取邮件的标题
                    message = self.entry_message.get()                                              # 获取邮件的内容
                    attachment = self.entry_attachment.get()                                        # 获取邮件的附件地址
                    email_emaillist = self.entry_email_list.get()                                   # 获取邮件的收件人

                    self.email =Sendemail(email_email_account, email_password)                      # 生成一个发邮件类的实例
                    attachment_list = attachment.strip("\n").strip('').split("#")              # 去除附件地址的\n,和空格,在切割储存在列表中
                    reciver_list = email_emaillist.strip("\n").strip('').split("#")                 # 同附件地址
                    messsages = message                                                             # 将获取的标题赋值,不能直接传递
                    title1 = title                                                                  # 将获取的内容赋值,不能直接传递

                    try:
                        '''尝试发送邮件 ,将获得的附件地址,收件人列表,内容标题传递'''
                        self.email.send(attachment_list, reciver_list, messsages, title1, )
                        '''发送成功生成一个发送成功的窗口'''
                        view_successful = tkinter.Tk()
                        view_successful.title("succeed")
                        view_successful.geometry("200x150+600+450")
                        label_successful = tkinter.Label(view_successful, text="发送成功", anchor=tkinter.CENTER)
                        label_successful.place(x=75, y=50)
                        view_successful.mainloop()

                    except:
                        '''发送失败弹出一个失败的窗口'''
                        view_error = tkinter.Tk()
                        view_error.title("error")
                        view_error.geometry("200x150+600+450")
                        label_error = tkinter.Label(view_error, text="遇到了预期之外的错误", fg="red", anchor=tkinter.CENTER)
                        label_error.place(x=75, y=50)
                        view_error.mainloop()

            win3 = Afterview()                                                                          # 生成一个登录成功后的实例
            win3.showview()                                                                             # 调用展示窗口的函数
        except:
            '''密码错误,生成一个提示密码错误的窗口'''
            view_error = tkinter.Tk()
            view_error.title("error")
            view_error.geometry("200x150+600+450")
            label_error = tkinter.Label(view_error, text="密码错误", anchor=tkinter.CENTER)
            label_error.place(x=75, y=50)
            view_error.mainloop()


window = Inputview()                                             # 产生登录界面的实例
window.showview()                                               # 调用登录界面中的展示函数


下面的为个人使用无需登录操作

在第八行sender处填入自己qq号,在password处填入自己的SMTP授权码即可使用

import tkinter
import smtplib
from email.mime.text import MIMEText                                # 邮件文本
from email.mime.multipart import MIMEMultipart


class Sendemail:
    def __init__(self, sender="###########", password="########", smtsever="smtp.qq.com",):
        self.smtsever = smtsever                                    # 服务器名称
        self.__sender = sender+"@qq.com"                            # 发送人者邮箱
        self.__password =password                                   # 发送者的SMTP授权码,具体获得可自行百度
        self.mailsever = smtplib.SMTP(self.smtsever, 25)            # 邮件服务器端口25
        self.mailsever.login(self.__sender, self.__password)        # 登录

    def send(self,filepath, maillist, message=None, title=None):    # 接受的参数有路径列表,收件人列表,内容和主题默认为空
        msg = MIMEMultipart()                                       # 创建MIMEMultipart的类
        for mail in maillist:                                       # 判断邮箱列表是否为空,如果是就加入自身账号
            if mail == '':
                maillist.append(self.__sender)
        if message != '':                                           # 如果message不为空就转化成邮件文本
            msg.attach(MIMEText(message))
        msg["Subject"] = title                                      # 将title设为邮件标题
        msg["From"] = self.__sender
        for path in filepath:                                       # 对于输入的所有附件路径进行遍历,如果为空就结束
            if path == '':
                continue
            else:
                att1 = MIMEText(open(path, 'rb').read(), "base64", "utf-8")     # 如果不为空就添加附件的
                att1["Content-Type"] = "application/octet-stream"
                filename = path[path.rfind("\\")+1:]                            # 因为输入的是绝对路径,所以返回最后一个\得出文件名称
                # att1["Content-Disposition"] = 'attachment ;filename = %s' % filename         #如果为全英文文件可以用这种方式
                att1.add_header('Content-Disposition', 'attachment', filename=('gbk', '', filename))    # 这种方式适合中英文文件
                msg.attach(att1)                                                # 每读取一个文件都加入到附件列表中
        self.mailsever.sendmail(self.__sender,                                  # 发送邮件,发邮件人
                                maillist,                                       # 邮件列表
                                msg.as_string())                                # 邮件的所有内容
        self.mailsever.quit()                                                   # 退出登录


class Afterview:

    def __init__(self):
        self.win1 = tkinter.Tk()                                    # 创建窗口
        self.win1.title("发邮件")                                   # 窗口标题
        self.win1.geometry("800x600+200+200")                       # 窗口大小和位置

    def showview(self):                                             # 展示窗口
        self.labels()                                               # 生成标签
        self.entrys()                                               # 生成输入框
        self.button_send()                                          # 生成按钮

        self.win1.mainloop()                                        # 窗口循环

    def labels(self):
        label_title = tkinter.Label(self.win1, text="请输入邮件主题:")             # 标签函数,提示输入主题
        label_title.place(x=100, y=100)                                             # 该标签的显示位置
        label_message = tkinter.Label(self.win1, text="请输入邮件内容:")           # 提示输入邮件内容
        label_message.place(x=100, y=200)                                           # 该标签的显示位置
        label_attachment = tkinter.Label(self.win1, text="请输入邮件附件绝对路径(#号键分割多个附件地址):")
        label_attachment.place(x=100, y=300)                                        # 提示输入附件位置,放置
        label_emaillist=tkinter.Label(self.win1, text="请输入邮件附件绝对路径(#分割多个收件人):")
        label_emaillist.place(x=100, y=400)                                         # 提示输入收件人邮箱,放置

    def entrys(self):                                                               # 标签函数
        self.entry_title = tkinter.Entry(self.win1, width=200)                       # 生成一个全局的输入框,一边其他函数使用
        self.entry_title.place(x=100, y=150)                                        # 标题输入框,放置
        self.entry_message = tkinter.Entry(self.win1, width=200)                     # 生成内容输入框
        self.entry_message.place(x=100, y=250)                                      # 放置的位置
        self.entry_attachment = tkinter.Entry(self.win1, width=200)                  # 生成附件输入框
        self.entry_attachment.place(x=100, y=350)                                   # 放置的位置
        self.entry_email_list = tkinter.Entry(self.win1, width=200)                  # 生成收件人输入框
        self.entry_email_list.place(x=100, y=450)                                   # 放置的位置

    def button_send(self):                                                          # 生成按钮函数
        button_send = tkinter.Button(self.win1, text="发送", command=self.sendemail)  # 生成一个发送按钮,绑定发送函数
        button_send.place(x=150, y=475)                                               # 放置

    def sendemail(self):                                                                # 发送邮件函数
        title = self.entry_title.get()                                                  # 获取邮件的标题
        message = self.entry_message.get()                                              # 获取邮件的内容
        attachment = self.entry_attachment.get()                                        # 获取邮件的附件地址
        email_emaillist = self.entry_email_list.get()                                   # 获取邮件的收件人

        self.email = Sendemail()                                                         # 生成一个发邮件类的实例
        attachment_list = attachment.strip("\n").strip('').split("#")                   # 去除附件地址的\n,和空格,在切割储存在列表中
        reciver_list = email_emaillist.strip("\n").strip('').split("#")                 # 同附件地址
        messsages = message                                                             # 将获取的标题赋值,不能直接传递
        title1 = title                                                                  # 将获取的内容赋值,不能直接传递

        try:
            '''尝试发送邮件 ,将获得的附件地址,收件人列表,内容标题传递'''
            self.email.send(attachment_list, reciver_list, messsages, title1, )
            '''发送成功生成一个发送成功的窗口'''
            view_successful = tkinter.Tk()
            view_successful.title("succeed")
            view_successful.geometry("200x150+600+450")
            label_successful = tkinter.Label(view_successful, text="发送成功", anchor=tkinter.CENTER)
            label_successful.place(x=75, y=50)
            view_successful.mainloop()

        except:
            '''发送失败弹出一个失败的窗口'''
            view_error = tkinter.Tk()
            view_error.title("error")
            view_error.geometry("200x150+600+450")
            label_error = tkinter.Label(view_error, text="遇到了预期之外的错误", fg="red", anchor=tkinter.CENTER)
            label_error.place(x=75, y=50)
            view_error.mainloop()


win3 = Afterview()                                                                          # 生成一个登录成功后的实例
win3.showview()                                                                             # 调用展示窗口的函数



本人初学,略显粗糙

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值