django - 在 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>')
解决方案
这是因为几个原因。其中更常见的是,如果您的允许不太安全的应用程序功能已关闭。
这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/。已经有太多天了,但我仍然喜欢通过指向你来回答(即使你已经修复了它)。
推荐阅读
- python - 为什么这个函数不返回任何输出而只使用打印?
- ajax - Laravel 中的 AJAX POST 请求说找不到 url
- android - 如何将 sklearn 模型嵌入手机以供离线使用?
- ios - 在笔记本电脑睡眠期间运行 applescript 以错误结束
- visual-studio-code - 如何在 VSCode 1.55+ 中关闭窗口标题 git 编辑状态
- android - 如何从 Firebase Cloud Functions 上传文本文件?
- visual-studio-2019 - C# & Visual Studio:存储问题
- node.js - 云函数中的参数未定义
- javascript - 如何将月份转换为单词而不是reactJs中的数字?
- html - 如何在 CSS 中定位滚动而不考虑它的块