使用gmail和python发送邮件时的SMTPAuthenticationError [重复]

时间:2022-11-15 12:17:50

This question already has an answer here:

这个问题在这里已有答案:

when i try to send mail using gmail and python error occurred this type of question are already in this site but doesn't help to me

当我尝试使用gmail发送邮件并发生python错误时,此类问题已经存在于此站点中,但对我没有帮助

gmail_user = "me@gmail.com"
gmail_pwd = "password"
TO = 'friend@gmail.com'
SUBJECT = "Testing sending using gmail"
TEXT = "Testing sending mail using gmail servers"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
BODY = '\r\n'.join(['To: %s' % TO,
        'From: %s' % gmail_user,
        'Subject: %s' % SUBJECT,
        '', TEXT])

server.sendmail(gmail_user, [TO], BODY)
print ('email sent')

error:

错误:

    server.login(gmail_user, gmail_pwd)
    File "/usr/lib/python3.4/smtplib.py", line 639, in login
   raise SMTPAuthenticationError(code, resp)
   smtplib.SMTPAuthenticationError: (534, b'5.7.14   
   <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtl1\n5.7.14       Li2yir27TqbRfvc02CzPqZoCqope_OQbulDzFqL-msIfsxObCTQ7TpWnbxIoAaQoPuL9ge\n5.7.14 BUgbiOqhTEPqJfb02d_L6rrdduHSxv26s_Ztg_JYYavkrqgs85IT1xZYwtbWIRE8OIvQKf\n5.7.14 xxtT7ENlZTS0Xyqnc1u4_MOrBVW8pgyNyeEgKKnKNyxce76JrsdnE1JgSQzr3pr47bL-kC\n5.7.14 XifnWXg> Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 fl15sm17237099pdb.92 - gsmtp')    

3 个解决方案

#1


79  

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:

编辑:Google阻止来自不使用现代安全标准的应用程序的登录尝试(在其支持页面上提到)。但是,您可以通过以下链接打开/关闭此安全功能:

Go to this link and select Turn On
https://www.google.com/settings/security/lesssecureapps

转到此链接,然后选择启用https://www.google.com/settings/security/lesssecureapps

#2


4  

Your code looks correct but sometimes google block an ip when you try to send a email since a unusual location, so, you can unblock in the next link

您的代码看起来是正确的,但有时当您尝试从不寻常的位置发送电子邮件时谷歌会阻止IP,因此,您可以在下一个链接中取消阻止

https://support.google.com/accounts/answer/6009563 and clicked in accounts.google.com/DisplayUnlockCaptcha .

https://support.google.com/accounts/answer/6009563并点击accounts.google.com/DisplayUnlockCaptcha。

#3


3  

I have just sent an email with gmail through Python. Try to use smtplib.SMTP_SSL to make the connection. Also, you may try to change the gmail domain and port.

我刚刚通过Python发送了一封包含gmail的电子邮件。尝试使用smtplib.SMTP_SSL进行连接。此外,您可能会尝试更改Gmail域和端口。

So, you may get a chance with:

所以,你可能有机会:

server = smtplib.SMTP_SSL('smtp.googlemail.com', 465)
server.login(gmail_user, password)
server.sendmail(gmail_user, TO, BODY)

As a plus, you could check the email builtin module. In this way, you can improve the readability of you your code and handle emails headers easily.

作为一个加号,您可以检查电子邮件内置模块。通过这种方式,您可以轻松提高代码的可读性并处理电子邮件标题。

#1


79  

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:

编辑:Google阻止来自不使用现代安全标准的应用程序的登录尝试(在其支持页面上提到)。但是,您可以通过以下链接打开/关闭此安全功能:

Go to this link and select Turn On
https://www.google.com/settings/security/lesssecureapps

转到此链接,然后选择启用https://www.google.com/settings/security/lesssecureapps

#2


4  

Your code looks correct but sometimes google block an ip when you try to send a email since a unusual location, so, you can unblock in the next link

您的代码看起来是正确的,但有时当您尝试从不寻常的位置发送电子邮件时谷歌会阻止IP,因此,您可以在下一个链接中取消阻止

https://support.google.com/accounts/answer/6009563 and clicked in accounts.google.com/DisplayUnlockCaptcha .

https://support.google.com/accounts/answer/6009563并点击accounts.google.com/DisplayUnlockCaptcha。

#3


3  

I have just sent an email with gmail through Python. Try to use smtplib.SMTP_SSL to make the connection. Also, you may try to change the gmail domain and port.

我刚刚通过Python发送了一封包含gmail的电子邮件。尝试使用smtplib.SMTP_SSL进行连接。此外,您可能会尝试更改Gmail域和端口。

So, you may get a chance with:

所以,你可能有机会:

server = smtplib.SMTP_SSL('smtp.googlemail.com', 465)
server.login(gmail_user, password)
server.sendmail(gmail_user, TO, BODY)

As a plus, you could check the email builtin module. In this way, you can improve the readability of you your code and handle emails headers easily.

作为一个加号,您可以检查电子邮件内置模块。通过这种方式,您可以轻松提高代码的可读性并处理电子邮件标题。