smtplib.SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful')

smtplib.SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful')

我正在尝试使用 smtplib 在 python 2.7 中发送邮件。下面的代码非常简单:

import smtplib

def main(argv=None):

    sender = 'abc@gmail.com'
    receivers = ['xyz@gmail.com']      

    message = """
    This is a test e-mail message.
    """

    smtpObj = smtplib.SMTP('xyz@gmail.com',25)

    smtpObj.login('abc', 'pwd')       
    smtpObj.sendmail(sender, receivers, message)         
    print "Successfully sent email"


if __name__ == '__main__':
    main()

现在当我执行下面的代码时,我不断收到这个异常:

smtplib.SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful').

有同样的问题。

2 个选项,

尝试更改:

smtpObj = smtplib.SMTP('xyz@gmail.com',25)

至:

smtpObj = smtplib.SMTP('xyz@gmail.com',587)

另一种可能是您的登录信息不正确。在我的例子中,我使用的是交换,登录名不是电子邮件地址,而是用户名

这是我用于交换服务器的代码:

import smtplib

def sendmail():
    subject = 'message subject'
    to = 'mail@yourdomain.com'
    sender = 'bjorn@***.nl'
    smtpserver = smtplib.SMTP("mail.mymailserver.nl",587)
    user = 'myussername'
    password = 'mypassword'
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.ehlo
    smtpserver.login(user, password)
    header = 'To:' + to + '\n' + 'From: ' + sender + '\n' + 'Subject:' + subject + '\n'
    message = header + '\n This is my message'
    smtpserver.sendmail(sender, to, message)
    smtpserver.close()

实际上,当我尝试在 python 控制台上执行相同的语句时,我才知道密码不正确,这是由于不同的字符编码。

对于所有其他用户,请勿复制粘贴

出现这个问题的原因是密码应该是客户端授权码,而不是邮箱的登录密码

如果您仔细检查了您的凭据,则此问题出在您的 Gmail 上。 您可以执行以下步骤来解决它:

1.Enable IMAP and/or POP3:

 1. Go to the "Settings", e.g. click on the "Gears" icon and select
    "Settings".
 2. Click on "Forwarding and POP/IMAP".
 3. Enable "IMAP Access" and/or "POP Download"
  1. 允许安全性较低的应用登录您的 Gmail。
 1. Login with your gmail account and find "Allow less secure apps:"
    from [Here][1]
 2. Google manages security with your gmail account. You need to turn on "Allow 
      less secure apps:" and you will receive mail in your gmail account.
      [1]: https://myaccount.google.com/security#activity

低安全性方法是暂时的,我无法在生产中使用它,但我发现一篇文章使使用 OAuth 对 GMail 进行身份验证变得更容易 here 使用示例代码,它可以完美运行。

您也可以尝试将端口从 587 更改为 25。这对我有用。

为了完整回答这个问题,我多次遇到完全相同的错误 - 每次都是出于不同的原因。这个错误消息一直困扰着我,我有点变异成了这个错误消息的 self-proclaimed 专家。这里列出了我遇到的所有问题以及我是如何解决的。

  • 需要在您的 Google 管理员 (https://admin.google.com/ac/security/lsa)

    中启用“不太安全的应用程序”
  • 即使启用了上述功能,如果您在电子邮件中设置了 two-factor 身份验证设置,该服务也可能会失败。解决方案是创建一个应用程序专用密码 (https://myaccount.google.com/u/5/apppasswords),该密码必须用作 Python 中 smtplib 的密码。

  • 如果您是第一次使用未知 IP 执行此操作。可能有一个秘密验证码启动身份验证。您可能会看到实际上是 534 错误代码而不是 535,但我不能保证。您需要先单击 link 以清除验证码 (https://accounts.google.com/DisplayUnlockCaptcha)。您可能需要等待 10 分钟才能真正注册它。所以喝杯咖啡吧。

  • 最后,如果您没有打开正确的端口,您将收到相同的错误消息。我的端口 587 没有在 firewalld 中打开。我启用了 smtp 和 smtps 作为服务,但默认情况下它们仅覆盖端口 25 和 465。因此,如果您使用 587,请确保它已打开,例如对于 firewalld,sudo firewall-cmd --permanent --add-port=587/tcp 并重新启动 firewalld 服务。

  • 最后,如果您没有在服务器上正确设置 SSL,此邮件仍然会失败。在某些时候,我收到了错误消息,在进一步调查一些日志后,我发现它无法进行握手。

  • 最后更新:我不小心将 ehlo() 在我的代码中的错误位置,突然开始抛出 535 身份验证错误,即:

    会话 = smtplib.SMTP('smtp.gmail.com', 587)

    session.starttls() session.login(sender_address, sender_pass)

    session.ehlo("yourdomain.com") # <-- 确保这是在 starttls() 之后而不是在

    之前

就我个人而言,是 Outlook 因违反使用条款而阻止了我的帐户。我通过浏览器重新登录,一切正常。