SMTPAuthenticationError at / (535, b'5.7.8 Username and Password not accepted.

am soo frustrated at this error. for some days i have tried all possible troubleshoots(eg turning on less secure apps,2FA, unlocking captcha) still its firing the error. I will highly value your help. below is my code

Traceback

Environment:


Request Method: POST
Request URL: http://localhost:8000/

Django Version: 3.0.9
Python Version: 3.8.3
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'shield_sec']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/apple/projects/Django/ingabo_sec/shield_sec/views.py", line 17, in contact
    send_mail (
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/mail/__init__.py", line 60, in send_mail
    return mail.send()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/mail/message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
    new_conn_created = self.open()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 69, in open
    self.connection.login(self.username, self.password)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 734, in login
    raise last_exception
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 723, in login
    (code, resp) = self.auth(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 646, in auth
    raise SMTPAuthenticationError(code, resp)

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

Settings.py

# Email_settings 
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'shieldseclab@gmail.com'
EMAIL_HOST_PASSWORD = '******'
EMAIL_USE_TLS = True 
#ACCOUNT_EMAIL_VERIFICATION = 'none'
#DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
#EMAIL_USE_SSL = True

Views.py

def contact (request):  
    if request.method == "POST":
        sender_name = request.POST.get('sender-name',False)
        sender_number = request.POST.get('sender-number',False) 
        sender_email = request.POST.get('sender-email',False)
        sender_subject = request.POST.get('sender-subject',False)
        message = request.POST.get('message',False)

        send_mail (
            sender_name, 
            sender_number,
            sender_email,
            ['shieldseclab@gmail.com'],
            sender_subject,
            message,
            )

        return render (request,'shield_sec/contact.html', {'sender_name': sender_name}) 
    else:
        return render (request,'shield_sec/contact.html', {})

Have you considered using an email provider such as sendgrid, mailgun, postmark, etc? This error is definitely a Google configuration/access problem. Unfortunately, I don’t have any great suggestions there.

thanks for your information. i will look up to them)

Unless you’re using a custom send_mail method, your calling sequence doesn’t match what the docs say: send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None, html_message=None).

Second, and I believe I had to confirm this in an earlier thread on this forum, GMail will not allow you to send emails from an arbitrary “from” address without authenticating / authorizing it through GMail. (Last I checked, you had to prove that you had control over the email address you’re using as the “from” address.) So, if you want emails to appear to come from something other than the account name you’re using to authenticate, you’ve got to go through the verification process for that email address.
Want to send emails from 100 different addresses? Then you would need to go through that verification process 100 times.

1 Like

thanks alot for your elaborated explanation. Fixed)