Django不能发送邮件,但smtplib可以

jslywgbw  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(106)

我尝试使用django的账户系统并配置了一些模板。当我试图重置密码时,它说“SMTPRecipientsRefused at /accounts/password_reset/”。但是当我用smtplib发送电子邮件时,它可以工作。
在django设置中:

# EMAIL
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "mail.X.com"
EMAIL_PORT = 587
EMAIL_HOST_USER = "X@mydomain"
EMAIL_PASSWORD = "password"

字符串
错误:

Exception Type: SMTPRecipientsRefused at /accounts/password_reset/
Exception Value: {'X@gmail.com': (450, b'4.7.1 We do not relay gmail.com')}


但是我知道我以前用smtplib用过同样的邮件,又试了一次,成功了。

>>> import smtplib
>>> from email.message import EmailMessage
>>> mail_server = "mail.X.com"
>>> mail_username = "X@mydomain"
>>> mail_password = "password"
>>> mail_port = 587
>>> msg = EmailMessage()
>>> msg["Subject"] = "subject test"
>>> msg["From"] = mail_username
>>> msg["To"] = "X@gmail.com"
>>> server = smtplib.SMTP(mail_server, mail_port)
>>> server.login(mail_username, mail_password)
(235, b'2.7.0 Ok')
>>> server.send_message(msg)
{}
>>> server.quit()
(221, b'2.0.0 Bye')


我尝试将DEFAULT_FROM_EMAIL = "X@mydomain"添加到settings.py,但没有任何区别。
我尝试添加EMAIL_USE_TLS=True,但它说:

SSLCertVerificationError at /accounts/password_reset/
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Hostname mismatch, certificate is not valid for 'mail.X.com'. (_ssl.c:1131)

pwuypxnk

pwuypxnk1#

正如之前在评论中所回复的,我建议更改这一行:

EMAIL_PASSWORD = "password"

字符串
对于此行:

EMAIL_HOST_PASSWORD = "password"

相关问题