SMTPAuthenticationError at /password-reset/

Question:

I am new in Django. I am trying to add a feature in my project that enables user to reset his password through given email. These configurations are in development and not production. This is my configuration in settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASS')

I tried these but nothing seems to be working. I enabled IMAP from gmail settings as well.
Can you please help me what am I doing wrong in here!
In case if you want to see full error.

SMTPAuthenticationError at /password-reset/ (535, b’5.7.8 Username and
Password not accepted.

https://support.google.com/mail/?p=BadCredentials a24sm3958276ljd.32 –
gsmtp’)

URL: http://localhost:8000/password-reset/
Exception Type: SMTPAuthenticationError
b’5.7.8 Username and Password not accepted. Learn more atn5.7.8
https://support.google.com/mail/?p=BadCredentials a24sm3958276ljd.32 –
gsmtp’) Exception
Location: C:UsersAdministratorAppDataLocalProgramsPythonPython37libsmtplib.py
in auth, line 642 Python
Executable: C:UsersAdministratorAppDataLocalProgramsPythonPython37python.exe
Python Version: 3.7.2 Python Path:
[‘C:UsersAdministratorDesktopdjango_project’,
‘C:UsersAdministratorAppDataLocalProgramsPythonPython37python37.zip’,
‘C:UsersAdministratorAppDataLocalProgramsPythonPython37DLLs’,
‘C:UsersAdministratorAppDataLocalProgramsPythonPython37lib’,
‘C:UsersAdministratorAppDataLocalProgramsPythonPython37’,
‘C:UsersAdministratorAppDataRoamingPythonPython37site-packages’,
‘C:UsersAdministratorAppDataLocalProgramsPythonPython37libsite-packages’]
Server time: Thu, 4 Apr 2019 12:24:22 +0000Learn more atn5.7.8Request Method: POST RequestDjango Version: 2.1.7Exception Value: (535,

Asked By: Mir Stephen

||

Answers:

Would you mind double checking if your environment variables are set correctly? Also, did you enable this feature on your Google account: https://www.google.com/settings/security/lesssecureapps?

Take a look at this answer, as it might help you as well: SMTPAuthenticationError when sending mail using gmail and python

Answered By: Marcelo Cardoso

In my case it was working on my local machine but not on heroku server. I went on this link https://accounts.google.com/DisplayUnlockCaptcha
and clicked on Continue. After that I had to re-login into my gmail account. It started working. Hope it might help someone!

Answered By: Himanshu Singh

Feb, 2022 Update:

You need to do 2 things to get over very strong google’s security.

  1. Allow less secure apps: ON ↓↓↓

    https://myaccount.google.com/lesssecureapps

  2. Allow access to your Google account: ON (Tap "Continue") ↓↓↓

    https://accounts.google.com/DisplayUnlockCaptcha

Answered By: Kai – Kazuya Ito