【问题标题】:Send email with python smtp error 535, b'5.7.3 Authentication unsuccessful'发送带有 python smtp 错误 535 的电子邮件,b'5.7.3 身份验证不成功'
【发布时间】:2018-01-07 03:26:56
【问题描述】:

我尝试使用 python smtp 库发送电子邮件:

import smtplib

to ="reciever@mail.adress.com"
user="sender@mail.adress.com"
password="password"
smtpserver = smtplib.SMTP("Outlook.mail.adress.com")
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(user,password)
header='To:'+to+'\n'+'From:'+user+'\n'+'Subject:test \n'
print (header)
msg = header+'\n test message \n'
smtpserver.sendmail(user,to,msg)
print ('done!')
smtpserver.quit()

它返回奇怪的错误,我找不到太多关于 (235, 503) 和 535 的信息

SMTPAuthenticationError...
     ...
---> 13 smtpserver.login(user,password)
     ...    

    728         # We could not login successfully.  Return result of last attempt.
--> 729         raise last_exception

C:\ProgramData\Anaconda3\lib\smtplib.py in login(self, user, password, initial_response_ok)
    718                 (code, resp) = self.auth(
    719                     authmethod, getattr(self, method_name),
--> 720                     initial_response_ok=initial_response_ok)
    721                 # 235 == 'Authentication successful'
    722                 # 503 == 'Error: already authenticated'

C:\ProgramData\Anaconda3\lib\smtplib.py in auth(self, mechanism, authobject, initial_response_ok)
    639         if code in (235, 503):
    640             return (code, resp)
--> 641         raise SMTPAuthenticationError(code, resp)
    642 
    643     def auth_cram_md5(self, challenge=None):

SMTPAuthenticationError: (535, b'5.7.3 Authentication unsuccessful')

如果我尝试在没有登录方法的情况下执行此操作,我会收到此错误:

---> 20 smtpserver.sendmail(user,to,msg)

--> 866             raise SMTPSenderRefused(code, resp, from_addr)

SMTPSenderRefused: (530, b'5.7.1 Client was not authenticated', 'xxx@mail.adress.com')

修复此代码需要做什么?

【问题讨论】:

  • 您确定需要密码才能发送电子邮件吗?你试过没有login 部分吗?
  • 如果你的意思是没有登录方法,那么是的,我会修改问题
  • 里面有密码或登录反斜杠吗?
  • 无反斜杠)

标签: python email smtp smtp-auth


【解决方案1】:

第二天我遇到了相同身份验证的问题。在我编写代码的第一天,它发送邮件非常好。但是我的company.com密码已过期,我不得不重置它,但重置后它也没有停止给出错误。经过2小时的来回后,它现在又可以发送邮件了。我改变的一件事是我手动编写了发件人和收件人的电子邮件 ID,而不是复制粘贴。其余的都是一样的。另外,由于我有一个由它组成的功能,我在使用输入“smail”拨打电话之前尝试了刷新功能。还要确保手动输入 smail 输入。请尝试并建议对您有用的方法。


def mailfile(smail):
    #smail='receiver@ymail.com'
    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.mime.base import MIMEBase
    from email import encoders
    fromaddr = "sender@company.com"
    toaddr = smail
    # instance of MIMEMultipart
    msg = MIMEMultipart()
    # storing the senders email address
    msg['From'] = fromaddr
    # storing the receivers email address
    msg['To'] = toaddr
    #storing the subject
    msg['Subject'] = "TEST MAIL"
    #string to store the body of the mail
    body = "Hi DID YOU GET MAIL???"
    #attach the body with the msg instance 
    msg.attach(MIMEText(body, 'plain')) 
    #open the file to be sent
    # PATH WHERE FILE IS BEING STORED
    filename = "filename_with_extension"
    attachment = open("Abra/ka/dabra/file_with_extension", "rb")
    #instance of MIMEBase and named as p
    p = MIMEBase('application', 'octet-stream')
    #To change the payload into encoded form 
    p.set_payload((attachment).read())
    #encode into base64
    encoders.encode_base64(p)
    p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
    #attach the instance 'p' to instance 'msg'
    msg.attach(p)
    #creates SMTP session
    s = smtplib.SMTP('smtp.office365.com',587)
    #start TLS for security
    s.starttls()
    s.ehlo()
    #Authentication
    s.login(fromaddr,'senders_passwd')
    #Converts the Multipart msg into a string
    text = msg.as_string()
    #sending the mail
    s.sendmail(fromaddr, toaddr, text)
    #terminating the session
    s.quit()

【讨论】:

    【解决方案2】:

    我在使用 Gmail 时遇到了这个问题,使用类似的代码进行测试,这并不完全是编程错误。 Google 刚刚阻止了来自未知设备的访问。

    【讨论】:

      猜你喜欢
      • 2019-01-26
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多