我正在尝试在Django中使用python使用G-Suite帐户发送电子邮件。由于Google停止了对新应用程序使用不太安全的App选项,因此我必须使用Oauth2。 但是当我开始通过smtplib发送电子邮件时,出现错误:
smtplib.SMTPSenderRefused: (530, b'5.7.0 Authentication Required. Learn more at\n5.7.0 https://support.google.com/mail/?p=WantAuthError q4sm8418287pfl.175 - gsmtp'
在查找引用后,它的意思是“ 530,“ 5.7.0”,必须首先发出STARTTLS命令”。 但是,我添加了“ server.starttls()”。有人可以帮我吗?非常感谢。
server = smtplib.SMTP('smtp.gmail.com', port=587)
server.ehlo('test')
server.starttls()
server.docmd('AUTH', 'XOAUTH2 ' + base64.b64encode(auth_string.encode()).decode("utf-8"))
server.sendmail(from_addr, to_addr, msg.as_string())
server.quit()
答案 0 :(得分:1)
根据此https://stackabuse.com/how-to-send-emails-with-gmail-using-python/#:~:text=As%20for%20the%20actual%20Python,com'%2C%20465)%20server.,您可能已启用了两步验证。
要使您的网络应用能够从gmail发送SMTP到SMTP,您需要为安全性较低的应用创建特定于应用的密码。
答案 1 :(得分:0)
EMAIL_BACKEND="django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST_USER ='youremail@gmail.com'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_PASSWORD = 'yourpassword'
EMAIL_USE_TLS = True
将此添加到您的设置中。之后:
python manage.py shell
您将进入Python shell。然后:
from django.core.mail import send_mail
在shell上以及之后编写此命令:
send_mail(
"django test mail",
"this is django test mail",
"your.email@gmail.com",
"recipient.email@example.com",
fail_silently=False
)
如果输出等于:
1
,然后您通过Gmail发送电子邮件。