SMTPAuthenticationError; Username and Password not accepted - python

I'm following along with this book and in chapter 18 part of the current assignment involves sending an email via Django. I have a yearly domain I use for testing, and rather than pay an additional 5$ a month I figured I'd just have the emails forwarded to an existing address, using these instructions.
I then went ahead and set it up so I could send mail using an alias via the Sending mail from your forwarded email address section
However, I'm unable to get it working using the generated app password + alias. When I try to send an email from the page it only works if 2FA is disabled and I use my actual gmail credentials, say for example:
EMAIL_HOST_USER = 'bob#gmail.com'
EMAIL_HOST_PASSWORD = 'bobswife123'
It works as intended. However, lets say I wanted to use my generated app password and alias, something like:
EMAIL_HOST_USER = 'alias#bob.com'
EMAIL_HOST_PASSWORD = 'bobsmistress123'
Then I'll get the following error:
Was not able to make any use of the support article from the URL in the above screenshot, but here it is for the sake of convenience:
https://support.google.com/mail/?p=BadCredentials
Anyways, these are the exact steps I'm taking:
From terminal on macOS 10.13.1, I run python manage.py runserver from terminal, and then enter http://localhost:8000/ into Google Chrome. The page displays correctly. I hit enter and try to send the email. Next, it either works, or I get the screenshot shown above. Here's a dpaste of the traceback:
http://dpaste.com/2DVFSPK
other potentially relevant settings:
EMAIL_HOST = 'smtp.gmail.com
EMAIL_PORT = 465
EMAIL_USE_SSL = True
Any help is greatly appreciated!
Regarding the duplication accusation:
I have already tried the accepted answer's instructions from the potential duplicate. Once again, I can get this to work by using my normal Gmail credentials -- but I don't want to reveal my personal email address, which is why I setup an alias using these instructions. This should allow me to send emails on behalf of the alias domain (which I own)
Edit: Adding my response to user inquiring whether I tried the troubleshooting steps (tl;dr yes i did):
Update your email client to the latest version.
Not using an email client for this; not applicable
Use an App Password: If you use 2-Step Verification, try signing in with an App Password.
Already addressed this above; kinda what my issue is about
Allow less secure apps: If you don't use 2-Step Verification, you might need to allow less secure apps to access your account.
Allow less secure apps is already enabled. Doesn't work otherwise
If you recently changed your Gmail password, you might need to re-enter your Gmail account information or completely repeat your Gmail account setup on your other email client.
Not related to my gmail password -- it works when I use my actual gmail credentials and I'm able to login to gmail
If the tips above didn't help, visit https://www.google.com/accounts/DisplayUnlockCaptcha and follow the steps on the page.
Already tried this
The email client's sign-in method might be insecure. Try signing in directly on the Gmail app.
Don't think I can use the gmail app in combination with this; not applicable

I encountered the same issue: I got it working by using different email addresses for login and sendmail (i.e. Login with gmail account and app password,then specify the forwarded email as sender in sendmail).
For example:
smtpObj.login('bob#gmail.com', 'bobsmistress123')
smtpObj.sendmail('alias#bob.com', receiver, emailString)

Related

What's solution for sending emails from python while gmail " the less secure apps " is not enabled anymore

I recently was using smtp library for sending emails from gmail account but recently it stopped working after research I found out the google can not let you enable the less secure app anymore . So is there any workaround this ?
If you want to continue using Gmail SMTP, you can set it up by setting an app password. An app password works like an alternate password for your account. It can only be used by the applications you share it with, so it’s more secure than sharing your primary password.
Here's how you can set it up: https://support.google.com/accounts/answer/185833?hl=en

Google disabled access to less secure apps

With google having disabled the access to enable access to less secure apps can anyone suggest ways in which I can send mails using flask-mail? It has to be disabled in the mail account I'm using to send mails using flask-mail.
If your problem is about google security you can use app password instead of your account password in MAIL_PASSWORD configuration, To do that you should enable 2-step verification in your Google Account and then use app password,
So go to your Google Account,
Security
Signing in to Google
Enable 2-step verification
App password
Make app password and use it as your MAIL_PASSWORD
In link below you can see other options
https://support.google.com/accounts/answer/6010255?hl=en&utm_source=google-account&utm_medium=profile-less-secure-apps-card
I had a similar problem using sendmail and ESMTP with Google Mail,
and the excellent 'sender_based_routing' patch from:
http://faculty.cs.niu.edu/~rickert/cf/
which allows sendmail to choose a different relay based on
email 'From:' headers; using my MS-365 corporate email selects
smtp.outlook365.com , while using my personal email selects
smtp.google.com.
Because I am out of mobile phone coverage while at home, I cannot
enable 2-Factor authentication or use an App Token, which requires
2-factor authentication and the ability to receive SMS; my
mobile phone package does not include SMS-over-WiFi, and
Google's 2-factor Auth SMS messages are not sent over Google Chat SMS.
The only workaround I have found is to open a new Google WorkSpaces
email account, then you can enable the SMTP relay feature, and
sending email with sendmail + ESMTP then works again.
Google has thus doubled its revenue stream from me; and since I must copy every message sent by my new account to my old one,
it has doubled my storage requirements, that I pay for.
Clever Google! Never neglects an opportunity to squeeze more
profit from its users.
Anyway, paying for 2 email accounts in order to send emails is
vastly preferable to be being forced to use the horrible GMail
GUI - I can use MA/GNUS Emacs to send mails - definitely worth it.
The best long term solution, which I will do as soon as I can,
is setup one's own Email server , and scrap use of Google altogether - I wish I was in a position to do this, as soon as
I am I will - it requires a static IP and an always-on server.
so you need to set up the App Password from setting.
This video shows how to set up App Password = link.
setps:
1. Manage your Google Account
2. Goto the Security. on left side
3.At security->Signing in to Google
1 first need on 2-setp-verfication.
2 then click App Password.then select app and device.
for me app=Mail.
device=Windows Compter.
3 A password is genrated. copy the password. It only show one time
4 used this password to login.
Note: Before proceeding please enable 2 factor authentication.
Less secure apps (https://myaccount.google.com/u/0/lesssecureapps) is no longer available.
We can use apppasswords feature provided by google via following link
https://myaccount.google.com/u/0/apppasswords
Check below image for reference, Use 16 digit code provided by google in-place of password.

Gmail login error with the help of imaplib in Python

I am trying to use imaplib in order to fetch my mails
import imaplib
mail= imaplib.IMAP4_SSL("imap.gmail.com")
mail.login("mymailaddress","mypassword")
mail.select("inbox")
but unfortunately, I am getting the following error
File , line 3, in <module>
mail.login("mymailaddress","mypassword")
raise self.error(dat[-1])
imaplib.error: b'[ALERT] Application-specific password required: https://support.google.com/accounts/answer/185833 (Failure)'
I have checked all other suggestion given on similar question but none are working (similar problem). I even enabled IMAP on my account. I also allow less secure app. What might be the reason for this error?
As stated in the error message you are getting You need to go to Sign in with App Passwords
The issue you are having is that you are using the users password. You should be using an apps password
Side note: Update as of May 30, 2022
To help keep your account secure, starting May 30, 2022, ​​Google will no longer support the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.
This means anyone connecting to the smtp / imap server and using the users password will get an error. Less secure apps will no work anymore.
You need to switch to using xoauth2 or trying an apps password. At this time it is not clear if apps password will work or not.
At this time it is not clear if apps password will work or not.
I was able to set up apps password today following an article in the google help center

imaplib not working with some outlook365 accounts

I am trying to connect to an email account with the imaplib python module but it doesn't work. With #outlook it works fine for me, but with #mydomain it doesn't work.
It's not a credential issue because I tried a POP connection and it worked, but not with imap.
I have searched in many places and it happens to many but I do not see a solution. Someone who can help me?
There's a few things to look at.
First, make sure IMAP access is enabled in the settings of whatever service you're using.
(Eg, Gmail: Settings > Forwarding and POP/IMAP > Enable IMAP). This is often not enough to fix the issue though, you need to set up an app password.
(Eg, Google: https://myaccount.google.com/security > Enable 2-step verification if it is off > App passwords).
An app password lets you sign into a service using a generated password in lieu of your main account password. Use this app password in your credentials while logging in with imaplib, and it should work. I'd suggest considering using a Microsoft app password too when using imaplib for Outlook, as it's more secure. You aren't storing your actual MS password in plain-text anywhere.
Hope that helps.

Not able to pass SendGrid verification with Django

I'm currently reading a book on Django (for beginners) and I reached the point where I need to implement a password reset feature to the test website using SendGrid SMTP Relay.
After creating an account and starting to create the SMTP relay I greeted with the following screen:
Based on this page I added the following lines of code to my setting.py file:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = 'xxxxx' #the string which is partialy hidden under the pink square
EMAIL_PORT = 587
EMAIL_USE_TLS = True
After running the website and trying to reset my password (the password of the superuser) I get the desired message with the reset link in my console but sadly nothing comes to my email. Thus I get the following error message when trying to verify integration.
What I tried so far:
I tried creating multiple different API keys to make sure nothing was wrong with the API key
I created a new SendGrid account
I tried removing EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' from the settings.py file (this only made things worst)
I've been trying to solve this issue for over a day now, any help would be greatly appreciated!
Your email backend setting is set to use the console. If you're just debugging that's fine, you can see how the emails would look like in the console and copy your password reset link from there.
If you really want to send an email, use the SMTP backend: Set EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'.
But beware, don't send emails to fake addresses using that, you'll get in trouble if you have too many bounces on your sendgrid account.
Also if you're going to use SendGrid in production, use the API instead of SMTP. django-anymail (but there are also other packages) provides a backend to use the API.
Update June 2021
Sendgrid doesn't allow simple username/password authentication anymore (and forces you to login with 2FA). You should use the API, or create an app password specifically for your server and use that instead of your normal password.
I'd say the settings are like they should be, taking into account that you of course don't intend to go into production without securing your info (ie creating environment variables). You are going to do this, I assume, but if not, you really, really should consider that.
Also it's these are your actual settings, you should edit this post right now. They're supposed to be kept secret, otherwise you're gonna have a real bad day shortly.
Given the nature of your problem, I'm halfway expecting that you're not testing it from a development setting, but rather that you've tried to run it from production. It is also my suspicion, that you're using Pythonanywhere or Heroku with a free account. Let me know if I'm all wrong here :)
The skinny is this: Without coughing up those $5, those two providers (and other providers add well, I presume) won't let you use sendgrid.
If I'm assuming wrongly, I'd appreciate seeing the views.py that handles the sending of email.

Categories