Skip to content

Support NTLM Auth for SMTP #58

@1letter

Description

@1letter

FEATURE REQUEST

Microsoft Exchange Server support NTLM Auth for SMTP. Let us support this option too.

Example:

import base64
import spnego
import smtplib
from smtplib import SMTPException, SMTPAuthenticationError
from email.message import EmailMessage

# the message
message = EmailMessage()
message.set_content("Hello World")

# SMTP server info
smtp_server = 'XXX'
smtp_port = 587
username = 'XXX'
password = 'XXX'
from = 'your-mail'
to = 'another-mail'

# Define ntlm authentication function
def ntlm_authenticate(smtp, username, password):
    auth = spnego.client(username, password, service="SMTP", protocol="ntlm")
    ntlm_negotiate = auth.step()

    code, response = smtp.docmd("AUTH", "NTLM " + base64.b64encode(ntlm_negotiate).decode())
    if code != 334:
        raise SMTPException("Server did not respond as expected to NTLM negotiate message")

    ntlm_challenge = base64.b64decode(response)
    ntlm_auth = auth.step(ntlm_challenge)
    code, response = smtp.docmd("", base64.b64encode(ntlm_auth).decode())
    if code != 235:
        raise SMTPAuthenticationError(code, response)

# Connect to smtp
smtp = smtplib.SMTP(smtp_server, smtp_port)
smtp.starttls()
smtp.ehlo_or_helo_if_needed()
ntlm_authenticate(smtp, username, password)
smtp.sendmail(from, to, message.as_string())

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions