I was trying to send mail using the gmail smtp in DJango. I know about this option Less secured apps
. But this option has been disabled by google now. Also I don't have any 2 factor authentication on my account.
Here is my setting.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]' # put your gmail address here
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True
But I am getting error
[2022-06-15 10:28:10,688: ERROR/ForkPoolWorker-1] Task app.tasks.send_email_task[9068931b-a457-415e-b910-d7c631596fce] raised unexpected: SMTPAuthenticationError(535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials o16-20020a056a00215000b0051b97828505sm9333570pfk.166 - gsmtp')
Traceback (most recent call last):
File "/etc/myprojectenv/lib/python3.8/site-packages/celery/app/trace.py", line 451, in trace_task
R = retval = fun(*args, **kwargs)
File "/etc/myprojectenv/lib/python3.8/site-packages/celery/app/trace.py", line 734, in __protected_call__
return self.run(*args, **kwargs)
File "/etc/myproject/app/tasks.py", line 12, in send_email_task
return send_mail(
File "/etc/myprojectenv/lib/python3.8/site-packages/django/core/mail/__init__.py", line 87, in send_mail
return mail.send()
File "/etc/myprojectenv/lib/python3.8/site-packages/django/core/mail/message.py", line 298, in send
return self.get_connection(fail_silently).send_messages([self])
File "/etc/myprojectenv/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 124, in send_messages
new_conn_created = self.open()
File "/etc/myprojectenv/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 91, in open
self.connection.login(self.username, self.password)
File "/usr/lib/python3.8/smtplib.py", line 743, in login
raise last_exception
File "/usr/lib/python3.8/smtplib.py", line 732, in login
(code, resp) = self.auth(
File "/usr/lib/python3.8/smtplib.py", line 655, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials o16-20020a056a00215000b0051b97828505sm9333570pfk.166 - gsmtp')
Less secure app option also has been disabled now
Now you need to generate the password for the mail app and select the device as windows computer.
This will generate a password. Provide this password in EMAIL_HOST_PASSWORD
. Try triggering the functionality to send mail again, this should work fine now.
CodePudding user response:
Google does not accept email with the less secure app anymore, As Himanshu says above An alternative way to send Gmail using an App password.