使用python smtp发送电子邮件错误535,b' 5.7.3身份验证失败'

时间:2017-07-31 13:55:07

标签: python email smtp smtp-auth

我尝试使用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')

修复此代码需要做些什么?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

第二天,我面临使用相同身份验证的问题。在第一天,当我编写代码时,它很好地发送了邮件。但是我的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()