Python 发送电子邮件时出错:引发 SMTPAuthenticationError(code, resp)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29351822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:27:51  来源:igfitidea点击:

Error sending email: raise SMTPAuthenticationError(code, resp)

pythonemailsmtplib

提问by UserX

Im trying to send emails with smtp module, but Im having an error:

我尝试使用 smtp 模块发送电子邮件,但出现错误:

File "/usr/lib/python2.7/smtplib.py", in login    
 raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (534, '5.7.14)...

Someone already had this error? Do you know how to fix?

有人已经有这个错误了吗?你知道怎么修吗?

The code:

编码:

def sendNotification():
    recepients_list = "[email protected]"
    subject = 'Subject'
    message = "Message" 
    sendemail(recepients_list,subject,message)

def sendemail(to_addr_list, subject, message):
    username = '[email protected]'
    password = 'passtest'   
    from_addr = '[email protected]'    
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login(username,password)
    newmessage = '\r\n'.join([
              'To: %s' %recepient_list,
               'From: %s' % from_addr,
                'Subject: %s' %subject,
                '',
                message
                ])
    try:    
        server.sendemail(from_addr, to_addr_list,newmessage)
        print 'notification sent'
    except:
        print 'error sending notification'
    server.quit()


sendNotification()

采纳答案by Joe Young

Go to Google's Account Security Settings: www.google.com/settings/security

转到 Google 的帐户安全设置: www.google.com/settings/security

Find the field "Access for less secure apps". Set it to "Allowed".

找到“访问不太安全的应用程序”字段。将其设置为“允许”。

Try your script again, changing server.sendemail()to server.sendmail()

再次尝试您的脚本,更改server.sendemail()server.sendmail()

回答by xiyu liu

(534, b'5.7.14 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/78754h16sm7090987wrc.89 - gsmtp')

(534, b'5.7.14 请通过您的网络浏览器登录,\n5.7.14 然后重试。\n5.7.14 了解更多信息\n5.7.14 https://support.google.com/mail/answer/78754h16sm7090987wrc.89 - gsmtp')

try https://myaccount.google.com/security#connectedapps

试试 https://myaccount.google.com/security#connectedapps

Allow less secure apps: ON Some apps and devices use less secure sign-in technology, which could leave your account vulnerable. You can turn off access for these apps (which we recommend) or choose to use them despite the risks.

允许安全性较低的应用程序:开 某些应用程序和设备使用安全性较低的登录技术,这可能会使您的帐户容易受到攻击。您可以关闭对这些应用程序的访问(我们建议这样做)或选择使用它们,尽管存在风险。

回答by user8861267

I had exactly the same problem. Yes, it worked. By enabling your gmail account security setting -> Allow less secure app, I was able to send a simple email from one gmail to another gmail account.

我遇到了完全相同的问题。是的,它奏效了。通过启用您的 Gmail 帐户安全设置 -> 允许安全性较低的应用程序,我能够从一个 Gmail 向另一个 Gmail 帐户发送一封简单的电子邮件。

WARNING:Allowing low security for apps accessing your Google account is not recommended by google. It can be a security threat. Turn it OFF after the experiment.

警告:Google 不建议为访问您的 Google 帐户的应用设置低安全性。它可能是一个安全威胁。实验后将其关闭。