使用 python 发送电子邮件。谷歌禁用不太安全的应用程序

Iph*_*pha 8 python email ssl smtp google-chrome

我正在尝试使用 python 发送电子邮件。在 Google 禁用“不太安全的应用程序”之前,我的代码运行良好。我的电子邮件地址和密码都是正确的。

server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
serverEmail = "EMAILADDRESS"
serverPw = "QWERTY"
server.login(serverEmail, serverPw)
subject = "Rejection"
body = "Hi! You've been unfortunately declined access to our system."
message = f'Subject: {subject}\n\n{body}'
server.sendmail("EMAILADDRESS", doctorEmail['email'], message)
server.quit()
Run Code Online (Sandbox Code Playgroud)

我现在收到此错误:

smtplib.SMTPAuthenticationError: (535, b'5.7.8 用户名和密码不被接受。

当我使用时出现此错误server.starttls()

smtplib.SMTPNotSupportedError:服务器不支持 STARTTLS 扩展。

小智 15

2-step verification打开然后前往App password

在此输入图像描述

生成密码后

import smtplib 
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as connection:  
    email_address = 'your_email_sender@gmail.com'
    email_password = 'App_Passwords_is_generated'
    connection.login(email_address, email_password )
    connection.sendmail(from_addr=email_address, to_addrs='receiver_email@something.com', 
    msg="subject:hi \n\n this is my message")
Run Code Online (Sandbox Code Playgroud)


Fir*_*ord 6

这对我有用。您需要为此生成一个应用程序密码。请参阅https://support.google.com/accounts/answer/185833?hl=en

import smtplib as smtp

connection = smtp.SMTP_SSL('smtp.gmail.com', 465)
    
email_addr = 'my_email@gmail.com'
email_passwd = 'app_password_generated_in_Google_Account_Settings'
connection.login(email_addr, email_passwd)
connection.sendmail(from_addr=email_addr, to_addrs='recipient@something.com', msg="Sent from my IDE. Hehe")
connection.close()
Run Code Online (Sandbox Code Playgroud)

由于某种原因,我的所有电子邮件最终都进入收件人帐户的垃圾邮件文件夹中。