i want to include sending notifications mails through gmail in my python code.
I followed all the steps to do it:
- import smtplib
- Enable less secure app
- Wait for 1 day
- Set 2-verification access
- Create 16 digits app password
- Change my gmail password with the 16 digits app password
- Log into my gmail account
- Unlock Display Captcha
- Run the code within 10 minutes
I still got the same error:
(534, b'5.7.9 Application-specific password required. Learn more at\n5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor h25sm8001063qkg.87 - gsmtp')
This is my code:
import smtplib, ssl
port = 465 # For SSL or 465
smtp_server = "smtp.gmail.com"
sender_email = "my_mail@gmail.com" # Enter your address
receiver_email = "my_mail@gmail.com" # Enter receiver address
password = '16digtisapppass' # i've checked I can log in to my gmail account with it
message = """\
Subject: Hi there
This message is sent from Python."""
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', port)
server.ehlo()
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
except Exception as e:
print(e)
# or
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
I've read these other posts:
- https://stackabuse.com/how-to-send-emails-with-gmail-using-python/
- http://stackoverflow.com/a/27515833/2684304
I'm not a professional coder, so maybe there is something simple that i'm missing.
I don't know what else could I do. Could anyone help me?