首页 > 解决方案 > 在 Ubuntu 16.04 中使用 Django send_mail 从 office365 发送电子邮件时出现 SMTPSenderRefused 错误?

问题描述

我在 ubuntu 16.04 DigitalOcean 中使用 django send_mail 从 office 365(Godaddy) 发送邮件时遇到一个问题。

这是我的实际配置:

EMAIL_HOST = 'smtp.office365.com'
EMAIL_HOST_USER = "myuser"
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_USE_TLS = True
EMAIL_PORT = 587

发送邮件:

context = {}
subject = 'Verificación de registro con 1 clic'
txt_ = get_template("registration/emails/verify.txt").render(context)
from_email = settings.DEFAULT_FROM_EMAIL
recipient_list = [somemail@somemail.com]
html_ = get_template("registration/emails/verify.html").render(context)
sent_mail = send_mail(
    subject,
    txt_,
    from_email,
    recipient_list,
    html_message=html_,
    fail_silently=False,
)

这是引发的错误:

raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (530, b'5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM []', '=?utf-8?q?myuser?= <myemail@myemail.com>')

标签: djangooffice365digital-ocean

解决方案


这是因为几个原因。其中更常见的是,如果您的允许不太安全的应用程序功能已关闭。

https://support.google.com/accounts/answer/6010255可能对您非常有用。

谷歌说»

如果为您的帐户关闭了“不太安全的应用程序访问”,您可以将其重新打开。我们建议改用更安全的应用程序

这意味着,您只需直接访问https://myaccount.google.com/lesssecureapps并打开。

您还可以访问https://myaccount.google.com/security和 Ctrl+F/Command+F » Less secure app access,然后您可以在下面的附图中看到类似的内容。

在此处输入图像描述


我如何解决这个问题?

就我而言,我在集成django-db-mailer时遇到了错误。

SMTPSenderRefused: (530, b'5.5.1 Authentication Required. Learn more at\n5.5.1  https://support.google.com/mail/?p=WantAuthError z25sm26247003pfn.7 - gsmtp', 'rishikesh0011115067@gmail.com')

我通过更改 EMAIL_USER -> EMAIL_HOST_USER 设置变量来修复它。

现在,我得到的下一个错误和你的完全一样。

SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials m123sm26024478pfb.133 - gsmtp')

我访问了 https://myaccount.google.com/lesssecureapps并发现我的帐户已关闭允许不太安全的应用程序功能。只需切换按钮的状态并运行代码。

终于成功了。我希望,这可能对你有所帮助。您还可以检查您是否在苦苦挣扎https://devanswers.co/allow-less-secure-apps-access-gmail-account/。已经有太多天了,但我仍然喜欢通过指向你来回答(即使你已经修复了它)。


推荐阅读