title: Error 535 whilst trying to send email from Python and NTLM library
body: <p>I'm trying to successfully authenticate against an Exchange SMTP gateway using Python. </p>
  
  <p>I'm using the ntlm_auth module from <a href="https://github.com/jborean93/ntlm-auth" rel="nofollow noreferrer">https://github.com/jborean93/ntlm-auth</a> and Python 3.5.2.</p>
  
  <ol>
  <li>I can login to webmail using these credentials. I know they work.</li>
  <li>I can send email using PowerShell, I know that works.</li>
  </ol>
  
  <p>This is the code I am using. I've read the NTLM specifications and the server is not accepting the authenticated message. 5.7.3 is a generic error, is there a better way of handling this scenario?</p>
  
  <pre><code>from smtplib import SMTP
  from ntlm_auth.ntlm import Ntlm
  import socket
  from smtplib import SMTPException, SMTPAuthenticationError
  
  workstation = socket.gethostname().upper()
  
  def ntlm_authenticate(smtp, domain, username, password):
      code, response = smtp.docmd("AUTH", "NTLM")
      ntlm_context = Ntlm(ntlm_compatibility=2)
      if code != 334:
          raise SMTPException("Server did not respond as expected to NTLM negotiate message")
  
      code, response = smtp.docmd(ntlm_context.create_negotiate_message(domain, workstation).decode())
  
      if code != 334:
          raise SMTPException("Server did not respond as expected to NTLM challenge message")
  
      ntlm_context.parse_challenge_message(response)
  
      code, response = smtp.docmd(ntlm_context.create_authenticate_message(username, password,
                                                                           domain, workstation).decode())
      if code != 235:
          raise SMTPAuthenticationError(code, response)
  
  EXCHANGE_PASSWORD = 'ThisIsReallyMyPassword!'
  
  fromaddr = 'anthony.shaw@ourcompany.com'
  toaddrs = 'my.colleague@ourcompany.com'
  msg= 'hello world!'
  
  print("Message length is", len(msg))
  
  conn = SMTP('webmail.ourcompany.com')
  conn.set_debuglevel(1)
  conn.starttls()
  conn.ehlo()
  ntlm_authenticate(conn, 'DOMAINXXX', 'anthony.shaw', EXCHANGE_PASSWORD)
  conn.sendmail(fromaddr, toaddrs, msg)
  conn.quit()
  </code></pre>
  
  <p>Here is a trace of the session, with various crypto words mangled for security</p>
  
  <pre><code>Message length is 12
  send: 'ehlo Anthonys-MacBook-Pro-2.local\r\n'
  reply: b'250-sdfsdf.sdfd.cloud Hello [121.123.184.192]\r\n'
  reply: b'250-SIZE 37748736\r\n'
  reply: b'250-PIPELINING\r\n'
  reply: b'250-DSN\r\n'
  reply: b'250-ENHANCEDSTATUSCODES\r\n'
  reply: b'250-STARTTLS\r\n'
  reply: b'250-X-ANONYMOUSTLS\r\n'
  reply: b'250-AUTH NTLM\r\n'
  reply: b'250-X-EXPS GSSAPI NTLM\r\n'
  reply: b'250-8BITMIME\r\n'
  reply: b'250-BINARYMIME\r\n'
  reply: b'250-CHUNKING\r\n'
  reply: b'250 XRDST\r\n'
  reply: retcode (250); Msg: b'asd.sds.cloud Hello [121.123.184.192]\nSIZE 37748736\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\nSTARTTLS\nX-ANONYMOUSTLS\nAUTH NTLM\nX-EXPS GSSAPI NTLM\n8BITMIME\nBINARYMIME\nCHUNKING\nXRDST'
  send: 'STARTTLS\r\n'
  reply: b'220 2.0.0 SMTP server ready\r\n'
  reply: retcode (220); Msg: b'2.0.0 SMTP server ready'
  send: 'ehlo Anthonys-MacBook-Pro-2.local\r\n'
  reply: b'250-sdfdfs.dddd.cloud Hello [121.123.184.192]\r\n'
  reply: b'250-SIZE 37748736\r\n'
  reply: b'250-PIPELINING\r\n'
  reply: b'250-DSN\r\n'
  reply: b'250-ENHANCEDSTATUSCODES\r\n'
  reply: b'250-AUTH NTLM LOGIN\r\n'
  reply: b'250-X-EXPS GSSAPI NTLM\r\n'
  reply: b'250-8BITMIME\r\n'
  reply: b'250-BINARYMIME\r\n'
  reply: b'250-CHUNKING\r\n'
  reply: b'250 XRDST\r\n'
  reply: retcode (250); Msg: dd.bbb.cloud Hello [121.123.184.192]\nSIZE 37748736\nPIPELINING\nDSN\nENHANCEDSTATUSCODES\nAUTH NTLM LOGIN\nX-EXPS GSSAPI NTLM\n8BITMIME\nBINARYMIME\nCHUNKING\nXRDST'
  send: 'AUTH NTLM\r\n'
  reply: b'334 NTLM supported\r\n'
  reply: retcode (334); Msg: b'NTLM supported'
  send: 'TlRMTVNTUAABAAsdfsdfsdfsdfBQAoAAAAHAAcAC0AAAAGAbEdAAAAD0lUQUFTQU5USE9OWVMtTUFDQk9PSy1QUk8tMi5MT0NBTA==\r\n'
  reply: b'334 TlRMTVNTUAACAAAABwAHADgAAAA2gonilvWzl6ZwMogAAAAAAAAAALYAtgA/AAAABgLwIwAAAA9DU0ZNRERTAgAOAGMAcwBmAG0AZABkAHMAAQsdfdsdfsfRABEAFMARAAwADEARQAwADIABAAaAGMAcwBmAG0AZABkAHMALgBjAGsdfdsAAwA2AEEAVQAwADEARABEAFMARAAwADEARQAwADIALgBjAHMAZgBtAGQAZABzAC4AYwBsAG8AdQBkAAUAGgBjAHMAZgBtAGQAZABzAC4AYwBsAG8AdQBkAAcACAD5GqJRU3DSAQAAAAA=\r\n'
  reply: retcode (334); Msg: b'TlRMTVNTUAACAAAABwAHADgAAAA2gonilvWzl6ZwMogAAAAAAAAAALYAtgA/AAAABgLwIwAAAA9DU0ZNRERTAgAOAGMAcwBmAG0AZABkAHMAAQAaAEEAVQAwADEARABEAFMARAAwADEARQAwADIABAAaAGMAcwBmAG0AZABkAHMALgBjAGwAbwB1AGQAAwA2AEEAVQAwADEARABEAFMARAAwADEARQAwADIALgBjAHMAZgBtAGQAZABzAC4AYwBsAG8AdQBkAAUAGgBjAHMAZgBtAGQAZABzAC4AYwBsAG8AdQBkAAcACAD5GqJRU3DSAQAAAAA='
  send: 'TlRMTVNTUAADAAAAGAAYAHUAAAfdfdfdfQAAAAUABQBIAAAADAAMAE0AAAAcABwAWQAAABAAEAClAAAANoKJ4gYBsR0AAAAPSVRBQVNhbnRob255LnNoYXdBTlRIT05ZUy1NQUNCT09LLVBSTy0yLkxPQ0FM8yeaKY0RjtEAAAAAAAAAAAAAAAAAAAAAD6wAaUOQF3QLY4klpKRYBYlkbYjyBNMJRLW0ZRQqzhDumAsyVC1uXg==\r\n'
  reply: b'535 5.7.3 Authentication unsuccessful\r\n'
  reply: retcode (535); Msg: b'5.7.3 Authentication unsuccessful'
  Traceback (most recent call last):
    File "email_users.py", line 38, in &lt;module&gt;
      ntlm_authenticate(conn, 'DOMAINXXX', 'anthony.shaw', EXCHANGE_PASSWORD)
    File "email_users.py", line 24, in ntlm_authenticate
      raise SMTPAuthenticationError(code, response)
  smtplib.SMTPAuthenticationError: (535, b'5.7.3 Authentication unsuccessful')
  </code></pre>