【问题标题】:I keep on getting the error "TypeError: must be str, not bytes"我不断收到错误“TypeError:必须是 str,而不是字节”
【发布时间】:2019-05-28 07:51:36
【问题描述】:

我不是 python 新手,但我是 Tkinter 和 smtplib 模块的新手。我正在尝试设置自己的电子邮件界面,使用 tkinter 作为 GUI,并使用 smtplib 发送电子邮件。该程序从输入框中获取输入,然后使用它们发送电子邮件,但它一直抛出这个错误,我不知道如何修复它......

File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1699, in __call__
return self.func(*args)
File "/Users/jonbrain/PycharmProjects/GUI for Email/venv/The 
Propper GuI.py", line 39, in send_it
server.login(emailUser, user_Password)


`File"/Library/Frameworks/Python.framework/Versions/3.6/lib/
python3. 6/smtplib.py", 
line 721, in login
initial_response_ok=initial_response_ok)
File"/Library/Frameworks/Python.framework/Versions/3.6/lib/
python3.6/smtplib.py", line 631, in auth
(code, resp) = self.docmd("AUTH", mechanism + " " + response)
TypeError: must be str, not bytes

我做了一些研究;许多人有同样的问题,但情况完全不同(这需要不同的解决方法)。所以这是我的代码。

from tkinter import *
import smtplib


root = Tk()
root.title("Jon's Email Service")
root.geometry("800x400+0+0")

Label(root, text="Jon's Email Service", font=("arial", 
60,"bold"), fg="blue").pack()

Label(root, text="User's Email address {Has to be gmail} ",font= 
("arial", 20,), fg="black").pack()

One = Entry(root,width=40, bg="white")
One.pack()

Label(root, text="User's Gmail Password", font=("arial", 
20,),fg="black").pack()

Two = Entry(root, width=40, bg="white")
Two.pack()

Label(root, text="Email Recipient", font=("arial", 
20,),fg="black").pack()
Three = Entry(root,width=40, bg="white")
Three.pack()
Label(root, text="The Message", font=("arial", 
20,),fg="black").pack()

Four = Entry(root, width=60, bg="white")
Four.pack()

def send_it():
    email_resipient = Three.get()
    Label(root, text="Email Is Sent!", font=("arial", 20,), 
    fg="Red").pack()
    emailUser = One.get()
    user_Password = Two.get
    msg = Four.get()
    print(emailUser)
    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.starttls()
    server.login(emailUser, user_Password)
    server.sendmail(emailUser, email_resipient, msg)
    server.quit()

send = Button(root, text="Send", default='active', width = 40, 
bg = "lightblue",command = send_it).pack()

root.mainloop()

【问题讨论】:

  • 我想你会发现user_Password不是你想的那样。
  • user_Password 是我创建的变量,因此用户登录凭据不会被硬编码。
  • user_Password 不是字符串,但您正尝试像使用它一样使用它。将print(user_Password) 添加到您的代码中,您会发现它不是您所期望的。
  • 好的,我添加了print(user_Password),它返回的正是我在输入框中输入的内容。你在说什么?
  • 那是不可能的。在上面的代码中,user_Password 是一个函数,而不是一个字符串。您将 user_Password 设置为 Two.getTwo.get 是一个函数。您需要将user_Password 设置为函数的resultTwo.get()。注意尾随的()

标签: python tkinter smtplib


【解决方案1】:

快速浏览后,我看到这一行 -> user_Password = Two.get 缺少它末尾的括号。结果,可能没有提供密码,而是一个字节对象,无论如何都不是密码。 在我的测试中将行更改为user_Password = Two.get() 似乎对我有用

这应该可以解决问题提出的问题,尽管您最终可能会遇到一个新问题smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials 196sm197509481pfc.77 - gsmtp'),这超出了此问题的范围。

【讨论】:

  • 感谢您的回答。您提出的错误只是您的谷歌登录不起作用,例如您的密码与您的电子邮件不匹配,或者您的密码错误。
猜你喜欢
  • 2018-09-13
  • 2020-12-03
  • 1970-01-01
  • 2018-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多