Smtpauthenticationerror When Sending Mail Using Gmail and Python

SMTPAuthenticationError when sending mail using gmail and python

Your code looks correct. Try logging in through your browser and if you are able to access your account come back and try your code again.
Just make sure that you have typed your username and password correct

EDIT:
Google blocks sign-in attempts from apps which do not use modern security standards (mentioned on their support page). You can however, turn on/off this safety feature by going to the link below:

Go to this link and select Turn On
https://www.google.com/settings/security/lesssecureapps

How to send an email using python after Google's policy update on not allowing just username and password?

Here is a more precise answer with all the main steps. I hope it will help other people.

  1. Log in into your email account: https://myaccount.google.com

  2. Then go to the security part

Sample Image

Be sure that you have turn on two steps verification and click on "App password"

Sample Image


  1. After select email and the corresponding device

Sample Image


  1. It will generate a password that will look like this; it is this password that you have to use in your python script.

Sample Image


  1. This password will appear here; in this place, you will have the option to erase it (so it will be no longer be useful to connect to your email account).

Sample Image

Hope it will help other people!

Login Authentication failed with Gmail SMTP (Updated)

Google has disabled the ability to enable less secure apps as of May 2022. Because of this, the previous solution of enabling less secure apps is no longer valid.

Steps:

  1. Go into your sending email address and make your way to the settings.
  2. Find two-step authentication and enable it.
  3. Under two-step authentication there should be a tab labeled App passwords. Click on it then select mail as the app and your device of choice
  4. Use the password generated from the app password as the password for your Gmail account.

Credits to: Link to source

smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted)

Since You can't use the less secure app now as the deadline was 30th May 2022.
An alternative way to send Gmail via flask using an App password

Before generating the APP password your google account must enable the 2FA.
If the 2FA is enabled then you can hop on to the security section in the manage accounts and there you can see the APP password section.

For generating the APP password you also can read -> here

The below code I tried and it's working

I have used python another dependencies

pip install Flask-Mail

from flask import Flask
from flask_mail import Mail, Message

app = Flask(__name__)
mail= Mail(app)

app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'someone@gmail.com'
app.config['MAIL_PASSWORD'] = 'YOUR_APP_PASSWORD'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)

@app.route("/")
def index():
msg = Message('Hello', sender = 'someone@gmail.com', recipients = ['someone@gmail.com'])
msg.body = "Hello Flask message sent from Flask-Mail"
mail.send(msg)
return "Sent"

if __name__ == '__main__':
app.run(debug = True)

Error sending email: raise SMTPAuthenticationError(code, resp)

Go to Google's Account Security Settings:
www.google.com/settings/security

Find the field "Access for less secure apps".
Set it to "Allowed".

Try your script again, changing server.sendemail() to server.sendmail()



Related Topics



Leave a reply



Submit