简体   繁体   中英

I can't authenticate at gmail via python

I'm trying to send an email via gmail:

  msg = MIMEMultipart()
  msg["From"] = "to@fdsfds.com"
  msg["To"] = ", ".join(recipients_array)
  msg["Subject"] = subject
  msg.attach(MIMEText(msg_body))


  # server = smtplib.SMTP("smtp.gmail.com", 587)
  server.ehlo()
  server.starttls()
  server.ehlo()
  server.login(my_gmail_address, my_gmail_password) # error here

  server.sendmail(my_gmail_address, recipients, msg.as_string())
  server.close()

And the error is:

Traceback (most recent call last):
    # .......
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbuZg\n5.7.14 fsfdsfds-kK\n5.7.14 fdsfdsfds-CyIaJn4-TUONIr_gHn9\n5.7.14 gfdgfd-543543\n5.7.14 aaaa> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 d8sm43303847pas.14 - gsmtp')

I'm authenticated in a browser at gmail.

As explained here you might need to change your security settings to enable access for less secure apps.

Go to https://www.google.com/settings/security/lesssecureapps to turn on access to less secure apps.

If that doesn't fix the problem, you should read the information referenced in the traceback's error message: https://support.google.com/mail/answer/78754

It is most likely an issue with authentication like mhawke suggested.

For a list of common errors as well as a much more convenient library, have a look at yagmail .

Syntax would look like:

import yagmail
yag = yagmail.SMTP(username, password)
yag.send("toaddr@gmail.com", "this subject", "text content")

Full disclosure: I developed yagmail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM