<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://performiq.com/kb/index.php?action=history&amp;feed=atom&amp;title=Python_-_Using_NTLM</id>
	<title>Python - Using NTLM - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://performiq.com/kb/index.php?action=history&amp;feed=atom&amp;title=Python_-_Using_NTLM"/>
	<link rel="alternate" type="text/html" href="https://performiq.com/kb/index.php?title=Python_-_Using_NTLM&amp;action=history"/>
	<updated>2026-05-18T13:49:32Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://performiq.com/kb/index.php?title=Python_-_Using_NTLM&amp;diff=4729&amp;oldid=prev</id>
		<title>PeterHarding: Created page with &quot; =Some Info=  From - http://stackoverflow.com/questions/2916396/smtp-through-exchange-using-integrated-windows-authentication-ntlm-using-pytho  &lt;pre&gt; from smtplib import SMTPE...&quot;</title>
		<link rel="alternate" type="text/html" href="https://performiq.com/kb/index.php?title=Python_-_Using_NTLM&amp;diff=4729&amp;oldid=prev"/>
		<updated>2016-05-21T00:29:59Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; =Some Info=  From - http://stackoverflow.com/questions/2916396/smtp-through-exchange-using-integrated-windows-authentication-ntlm-using-pytho  &amp;lt;pre&amp;gt; from smtplib import SMTPE...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
=Some Info=&lt;br /&gt;
&lt;br /&gt;
From - http://stackoverflow.com/questions/2916396/smtp-through-exchange-using-integrated-windows-authentication-ntlm-using-pytho&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from smtplib import SMTPException, SMTPAuthenticationError&lt;br /&gt;
import string&lt;br /&gt;
import base64&lt;br /&gt;
import sspi&lt;br /&gt;
&lt;br /&gt;
# NTLM Guide -- http://curl.haxx.se/rfc/ntlm.html&lt;br /&gt;
&lt;br /&gt;
SMTP_EHLO_OKAY = 250&lt;br /&gt;
SMTP_AUTH_CHALLENGE = 334&lt;br /&gt;
SMTP_AUTH_OKAY = 235&lt;br /&gt;
&lt;br /&gt;
def asbase64(msg):&lt;br /&gt;
    return string.replace(base64.encodestring(msg), &amp;#039;\n&amp;#039;, &amp;#039;&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
def connect_to_exchange_as_current_user(smtp):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Example:&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; import smtplib&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; smtp = smtplib.SMTP(&amp;quot;my.smtp.server&amp;quot;)&lt;br /&gt;
    &amp;gt;&amp;gt;&amp;gt; connect_to_exchange_as_current_user(smtp)&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    # Send the SMTP EHLO command&lt;br /&gt;
    code, response = smtp.ehlo()&lt;br /&gt;
    if code != SMTP_EHLO_OKAY:&lt;br /&gt;
        raise SMTPException(&amp;quot;Server did not respond as expected to EHLO command&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    sspiclient = sspi.ClientAuth(&amp;#039;NTLM&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
    # Generate the NTLM Type 1 message&lt;br /&gt;
    sec_buffer=None&lt;br /&gt;
    err, sec_buffer = sspiclient.authorize(sec_buffer)&lt;br /&gt;
    ntlm_message = asbase64(sec_buffer[0].Buffer)&lt;br /&gt;
&lt;br /&gt;
    # Send the NTLM Type 1 message -- Authentication Request&lt;br /&gt;
    code, response = smtp.docmd(&amp;quot;AUTH&amp;quot;, &amp;quot;NTLM &amp;quot; + ntlm_message)&lt;br /&gt;
&lt;br /&gt;
    # Verify the NTLM Type 2 response -- Challenge Message&lt;br /&gt;
    if code != SMTP_AUTH_CHALLENGE:&lt;br /&gt;
        raise SMTPException(&amp;quot;Server did not respond as expected to NTLM negotiate message&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    # Generate the NTLM Type 3 message&lt;br /&gt;
    err, sec_buffer = sspiclient.authorize(base64.decodestring(response))&lt;br /&gt;
    ntlm_message = asbase64(sec_buffer[0].Buffer)&lt;br /&gt;
&lt;br /&gt;
    # Send the NTLM Type 3 message -- Response Message&lt;br /&gt;
    code, response = smtp.docmd(&amp;quot;&amp;quot;, ntlm_message)&lt;br /&gt;
    if code != SMTP_AUTH_OKAY:&lt;br /&gt;
        raise SMTPAuthenticationError(code, response)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Also looked promising:&lt;br /&gt;
&lt;br /&gt;
* http://stackoverflow.com/questions/909658/windows-authentication-with-python-and-urllib2&lt;br /&gt;
* https://github.com/mullender/python-ntlm&lt;br /&gt;
&lt;br /&gt;
[[Category:Python]]&lt;/div&gt;</summary>
		<author><name>PeterHarding</name></author>
	</entry>
</feed>