title: Gmail blocks login attempt from Python with app specific password
body: <p>I'm trying to send an email from a Google account using Python's smtplib, but getting an error, and now I'm kind of at a loss. Google responds with the following: <code>Please log in via your web browser and then try again. Learn more at https://support.google.com/mail/answer/78754</code>.</p>
  
  <p>The account has two factor authentication enabled, so I'm using an app specific password for my login. To my understanding, this should then work without enabling the setting for less secure apps, shouldn't it? I've been doing the same with another account while testing without a problem, but now I finally got the credentials for the proper account and there it won't accept the authentication.</p>
  
  <p>I'm aware that there is a Python Gmail API thingy to use with OAuth, but if at all possible I don't want to include more packages and rewrite much, and I don't really want to enable the "less secure apps" setting either.
  Is there a way to get this working without either?</p>
  
  <p>If it makes a difference, here is the code I use for sending email. As said before, this was working fine with another account, so I'm not sure if it's actually relevant.</p>
  
  <pre><code>def send_mail(to_address, subject, body):
      smtp_user = "myaccount@domain.com"
      smtp_password = "MyAppPasswordFromGoogle"
      server = "smtp.gmail.com"
      port = 587
  
      msg = MIMEMultipart("alternative")
      msg["Subject"] = subject
      msg["From"] = smtp_user
      msg["To"] = to_address
      msg.attach(MIMEText(body, "html"))
      s = smtplib.SMTP(server, port)
      s.connect(server, port)
      s.ehlo()
      s.starttls()
      s.ehlo()
      s.login(smtp_user, smtp_password)
      s.sendmail(smtp_user, to_address, msg.as_string())
      s.quit()
  </code></pre>
  
  <p>Edit:
  There is an interesting difference between the two accounts: on <a href="https://myaccount.google.com/lesssecureapps" rel="noreferrer">https://myaccount.google.com/lesssecureapps</a>, my old (working) one says "this setting isn't available for accounts that have two factor authentication enabled", while the new one says "this setting is managed by your domain administrator", even though both use 2FA and it's also forced in both domains. So I suppose there is some setting that the domain admin has to change, but I don't know which one that would be.</p>