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.
Log in into your email account: https://myaccount.google.com
Then go to the security part
Be sure that you have turn on two steps verification and click on "App password"
- After select email and the corresponding device
- It will generate a password that will look like this; it is this password that you have to use in your python script.
- 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).
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:
- Go into your sending email address and make your way to the settings.
- Find two-step authentication and enable it.
- 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
- 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
Reading Dynamically Generated Web Pages Using Python
Matplotlib Scatterplot with Legend
Python Opencv Line Detection to Detect 'X' Symbol in Image
How to Install Pip3 on Windows
Calling a Function Upon Button Press
Web Scraping Program Cannot Find Element Which I Can See in the Browser
Print Statement Inside of Input Returns with a "None"
Use Groupby in Pandas to Count Things in One Column in Comparison to Another
How to Take the Nth Digit of a Number in Python
List Comprehension and Lambdas in Python
Pandas Sum by Groupby, But Exclude Certain Columns
Getting Values from Object Oriented Tkinter
How Do Python Functions Handle the Types of Parameters That You Pass In
Is the Shortcircuit Behaviour of Python's Any/All Explicit
What Is the Most Compatible Way to Install Python Modules on a MAC