Login Credentials Not Working with Gmail Smtp

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)

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

Nodemailer: response: '535-5.7.8 Username and Password not accepted

Solved it by creating App password inside Google account. You must have 2-step verification actived.

2-step actived

App password created



Related Topics



Leave a reply



Submit