【问题标题】:Python SMTPAuthnticationErrorPython SMTPAuthenticationError
【发布时间】:2020-04-03 20:11:27
【问题描述】:

直到昨天我用 python 发送电子邮件都没有问题,现在由于某种原因我收到以下错误。

Traceback (most recent call last):
  File "main.py", line 20, in <module>
    mail(name)
  File "/home/runner/mail.py", line 26, in mail
    smtpserver.login(gmail_user, gmail_password)
  File "/usr/local/lib/python3.7/smtplib.py", line 730, in login
    raise last_exception
  File "/usr/local/lib/python3.7/smtplib.py", line 721, in login
    initial_response_ok=initial_response_ok)
  File "/usr/local/lib/python3.7/smtplib.py", line 642, in auth
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8  https://support.google.com/mail/?p=BadCredentials p17sm967082ils.71 - gsmtp')

我已经使用以下代码大约 4 个月了,没有遇到任何问题。这是我用来访问 gmail 的代码:

import smtplib

def mail(name):
    gmail_user = 'email@gmail.com'
    gmail_password = 'password'

    emaillist = ['email@email.com']

    for email in emaillist:
        sent_from = gmail_user
        to = email
        subject = 'Subject'

        body = "Body"

        email_text = '''\
        From: %s
        To: %s
        Subject: %s

        %s
        '''%(sent_from, to, subject, body)

        try:
            server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
            server.ehlo()
            server.login(gmail_user, gmail_password)
            server.sendmail(sent_from, to, body)
            server.close()

            print('Email sent!')
        except:
            print('Oops!')

我已尝试更改 gmail 中使用安全性较低的应用程序、启用 IMAP 并更改电子邮件帐户的密码。到目前为止,解决问题还没有运气。任何帮助将不胜感激。

【问题讨论】:

    标签: python smtp-auth


    【解决方案1】:

    Google 已增加其安全政策,我邀请您阅读以下部分中的此链接:不太安全的应用和您的 Google 帐户,然后像下面这样打开链接(不太安全的应用访问)

    https://support.google.com/accounts/answer/6010255

    稍后允许访问不安全的应用程序:是

    【讨论】:

    • 我试过切换它,但它仍然不起作用。
    • 但是你确定你是用同一个账号登录的吗?
    • 正在使用某些 IDE 来执行您的脚本?
    • 我每天都在使用 pythonanywhere 运行我的脚本,但一直在使用 repl 来尝试解决问题。我最初在 repl 中编写了整个代码,直到昨天我注意到它停止工作时,它在两个地方都有效。
    • 是否可以添加所有代码? ..with imports ..(就在你发送邮件的时候)
    【解决方案2】:

    这是我的代码,您需要将 smtp.gmail.com 端口设置为 587

    在 sendMail 中,我们有 警报通知 是主题和 有人进入房间,图片附件 是正文,如果需要,我们也有附件 [" /home/pi/webcam.jpg"]

    代码:

    #!/usr/bin/env python
    import smtplib
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
    from email.MIMEText import MIMEText
    from email.Utils import COMMASPACE, formatdate
    from email import Encoders
    import os
    
    USERNAME = "___YOUR SMTP EMAIL HERE___"
    PASSWORD = "__YOUR SMTP PASSWORD HERE___"
    
    def sendMail(to, subject, text, files=[]):
        assert type(to)==list
        assert type(files)==list
    
        msg = MIMEMultipart()
        msg['From'] = USERNAME
        msg['To'] = COMMASPACE.join(to)
        msg['Date'] = formatdate(localtime=True)
        msg['Subject'] = subject
    
        msg.attach( MIMEText(text) )
    
        for file in files:
            part = MIMEBase('application', "octet-stream")
            part.set_payload( open(file,"rb").read() )
            Encoders.encode_base64(part)
            part.add_header('Content-Disposition', 'attachment; filename="%s"'
                           % os.path.basename(file))
            msg.attach(part)
    
        server = smtplib.SMTP('smtp.gmail.com:587')
        server.ehlo_or_helo_if_needed()
        server.starttls()
        server.ehlo_or_helo_if_needed()
        server.login(USERNAME,PASSWORD)
        server.sendmail(USERNAME, to, msg.as_string())
        server.quit()
    
    sendMail( ["___EMAIL TO RECEIVE THE MESSAGE__"],
            "Alarm notification",
            "Someone has entered the room, picture attached",
            ["/home/pi/webcam.jpg"] )
    

    【讨论】:

    • 端口号是 smtplib.SMTP_SSL 的正确端口。我最终通过更改 gmail 帐户的密码解决了这个问题。
    猜你喜欢
    • 2016-11-18
    • 2012-11-02
    • 2014-12-29
    • 2016-04-29
    • 2014-10-25
    • 2022-11-03
    • 2021-03-14
    • 2019-08-26
    • 2018-01-12
    相关资源
    最近更新 更多