title: Django SMTPAuthenticationError
type: categories
date: 2017-07-29 09:58:38
categories: Python3
tags: [Django, Heroku, SMTP]
在Django应用中,利用SMTP发送验证邮件,本地的环境完全没问题,但当将Django应用部署到Heroku上后,邮件发送失败,报SMTPAuthenticationError错误。解决这个问题发费时间较长,记录一下。
这个问题针对【 Gmail 】邮。
setting.py
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'jmspvu@gmail.com'
EMAIL_HOST_PASSWORD = '*******'
DEFAULT_FROM_EMAIL = 'jmspvu@gmail.com'
views.py
try:
sendEmail = EmailMessage(subject='用户注册', body=message, to=[loginName])
sendEmail.send(fail_silently=False)
print(loginName)
msg = {"isSuccess": True,
"msg": 'Verification code sent successfully'}
return HttpResponse(json.dumps(msg), content_type='application/json')
except:
# msg = {"isSuccess": False,
# "msg": 'Verification code sent failure'}
# return HttpResponse(json.dumps(msg), content_type='application/json')
raise Exception
在Heroku上的报错
SMTPAuthenticationError
During handling of the above exception ((534, b'5.7.14 Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 q15sm15913999qkl.26 - gsmtp')), another exception occurred:
解决方法(Gmail)
1、检查账号、密码是否匹配;
2、开启gmail邮箱的不安全应用:https://myaccount.google.com/lesssecureapps
3、授权账号访问权限(我的就因为开启这个,浪费了那么多时间)https://accounts.google.com/DisplayUnlockCaptcha