1

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:

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?

David_Rowie
  • 127
  • 2
  • 10
  • Google seems to be blocking the attempt to login to your account from a less secure source. Follow the instructions here to enable access: https://stackoverflow.com/questions/16512592/login-credentials-not-working-with-gmail-smtp – jignatius Mar 22 '20 at 05:55
  • Also check you got the password right. – jignatius Mar 22 '20 at 08:12
  • Thank you for you comments. But the stackoverflow link points to the same i've already posted. I've enumerated all steps suggested by it, and it keep not working. The password is ok, i've changed intentionally and the error that shows is another one. Also I've logged in my gmail account by copying the 16 digits password from my code and pasting in it in the password input box, and it worked. – David_Rowie Mar 22 '20 at 18:48
  • Try to complete: https://accounts.google.com/b/0/DisplayUnlockCaptcha Check you have disabled 2 factor authentication. – jignatius Mar 23 '20 at 04:33
  • Thank you again, but I have already done that last step (i was in my enumeration). How could I check the 2 factor is disabled? If I run my code it still throws the 534 error and, by the other hand, google invites me to try again access my gmail account from my app.In other words, it is disabled... but It still does throws the 534 error. – David_Rowie Mar 23 '20 at 05:28
  • 1
    https://support.google.com/accounts/answer/1064203?co=GENIE.Platform%3DDesktop&hl=en – jignatius Mar 23 '20 at 05:30
  • It worked! thank you very much for your time and help! If you want it and it's important to you, post it like an answer and I'll give you the ok for your reputation points. Happy monday! – David_Rowie Mar 23 '20 at 13:59
  • 1
    I'm glad it worked and we got there in the end. – jignatius Mar 23 '20 at 14:41

2 Answers2

1

The problem is that the "2-Step Verification" is on your account.

Solutions:

  1. Turn off 2-Step Verification and use your normal password account in your app (see this link for more details)
  2. Sign in with App Passwords, An App Password is a 16-digit passcode that gives a less secure app or device permission to access your Google Account. App Passwords can only be used with accounts that have 2-Step Verification turned on (see this link or link2). enter image description here
csmaster
  • 579
  • 4
  • 14
0

gmail port is 587

you are using AT&T port 465 just replace with 587port .