我在发送电子邮件而不必启用Google不太安全的应用程序时遇到麻烦:https://myaccount.google.com/lesssecureapps。如何在Python中修改代码,以便无需启用此功能即可发送电子邮件?我需要禁用此选项,因为我正在构建一个应用程序,用户可以在其中向开发人员发送反馈电子邮件,因此不能强制用户启用安全性较低的应用程序。我在Python中的代码:
def send_email(self):
print("send e-mail to developer")
sender_email = self.email.text
sender_password = self.password.text
sender_feedback = self.feedback_message.text
print("email is: ", type(sender_email))
print("pass is: ", sender_password)
print("feedback message is: ", sender_feedback)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls() # encrypt connection
server.login(sender_email, sender_password)
subject = 'Feedback'
msg = sender_feedback
msg = f"Subject: {subject} \n\n {msg}"
server.sendmail(
# from,to,message
sender_email,
'[email protected]',
msg
)
print("E-MAIL HAS BEEN SENT!")
server.quit()
根据POP3/SMTP compatible with 2 factor authentication? - Help - Gmail,当您为Google帐户启用“两步验证”时,需要使用set up an "app password"代替常规密码才能使用SMTP客户端。