From 8572a6ce3f21a049840a9c66c92942b707a42592 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Sat, 21 Mar 2026 12:31:51 +0100 Subject: [PATCH] Support SmtpAuthenticationMode.Ntlm --- src/NLog.MailKit/MailTarget.cs | 21 +++++++++++++------ .../UnitTests/MailTargetTests.cs | 21 ------------------- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/src/NLog.MailKit/MailTarget.cs b/src/NLog.MailKit/MailTarget.cs index 05b281a..5066a1d 100644 --- a/src/NLog.MailKit/MailTarget.cs +++ b/src/NLog.MailKit/MailTarget.cs @@ -35,6 +35,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Linq; +using System.Net; using System.Text; using MailKit.Net.Smtp; using MailKit.Security; @@ -446,6 +447,19 @@ private void SendMailMessage(MimeMessage message, LogEventInfo lastEvent) var oauth2 = new SaslMechanismOAuth2(userName, oauth2Token); client.Authenticate(oauth2); } + else if (smtpAuthentication == SmtpAuthenticationMode.Ntlm) + { + var userName = RenderLogEvent(SmtpUserName, lastEvent); + var password = RenderLogEvent(SmtpPassword, lastEvent); + if (!string.IsNullOrWhiteSpace(userName)) + { + client.Authenticate(new SaslMechanismNtlm(userName, password)); + } + else + { + client.Authenticate(new SaslMechanismNtlm(CredentialCache.DefaultNetworkCredentials)); + } + } client.Send(message); InternalLogger.Trace("{0}: Sending mail done. Disconnecting", this); @@ -535,17 +549,12 @@ private void CheckRequiredParameters() throw new NLogConfigurationException("MailTarget - To address is required"); } - var smtpAuthentication = RenderLogEvent(SmtpAuthentication, LogEventInfo.CreateNullEvent()); - if (smtpAuthentication == SmtpAuthenticationMode.Ntlm) - { - throw new NLogConfigurationException("MailTarget - SmtpAuthentication NTLM not yet supported"); - } - if (IsEmptyLayout(PickupDirectoryLocation) && IsEmptyLayout(SmtpServer)) { throw new NLogConfigurationException("MailTarget - SmtpServer is required"); } + var smtpAuthentication = RenderLogEvent(SmtpAuthentication, LogEventInfo.CreateNullEvent()); if (smtpAuthentication == SmtpAuthenticationMode.OAuth2 && (IsEmptyLayout(SmtpUserName) || IsEmptyLayout(SmtpPassword))) { throw new NLogConfigurationException("MailTarget - SmtpUserName (OAuth UserName) and SmtpPassword (OAuth AccessToken) is required when SmtpAuthentication = OAuth2"); diff --git a/test/NLog.MailKit.Tests/UnitTests/MailTargetTests.cs b/test/NLog.MailKit.Tests/UnitTests/MailTargetTests.cs index f4a3f6f..c1c9cf5 100644 --- a/test/NLog.MailKit.Tests/UnitTests/MailTargetTests.cs +++ b/test/NLog.MailKit.Tests/UnitTests/MailTargetTests.cs @@ -189,27 +189,6 @@ public void MailTargetInitialize_WithoutSpecifiedSmtpServer_ThrowsConfigExceptio ); } - [Fact] - public void MailTargetInitialize_WithSmtpAuthenticationModeNtlm_ThrowsConfigException() - { - var mmt = new MailTarget - { - From = "foo@bar.com", - To = "bar@bar.com", - Subject = "Hello from NLog", - SmtpServer = "server1", - SmtpPort = 27, - SmtpAuthentication = SmtpAuthenticationMode.Ntlm, - }; - - Assert.Throws(() => - new LogFactory().Setup().LoadConfiguration(cfg => - { - cfg.Configuration.AddRuleForAllLevels(mmt); - }) - ); - } - [Fact] public void MailTargetInitialize_WithSmtpAuthenticationModeOAuth2_ThrowsConfigException() {