Skip to content

Commit e0dd9b3

Browse files
authored
Support SmtpAuthenticationMode.Ntlm (#244)
1 parent 549c04b commit e0dd9b3

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

src/NLog.MailKit/MailTarget.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
using System.Collections.Generic;
3636
using System.ComponentModel;
3737
using System.Linq;
38+
using System.Net;
3839
using System.Text;
3940
using MailKit.Net.Smtp;
4041
using MailKit.Security;
@@ -446,6 +447,19 @@ private void SendMailMessage(MimeMessage message, LogEventInfo lastEvent)
446447
var oauth2 = new SaslMechanismOAuth2(userName, oauth2Token);
447448
client.Authenticate(oauth2);
448449
}
450+
else if (smtpAuthentication == SmtpAuthenticationMode.Ntlm)
451+
{
452+
var userName = RenderLogEvent(SmtpUserName, lastEvent);
453+
var password = RenderLogEvent(SmtpPassword, lastEvent);
454+
if (!string.IsNullOrWhiteSpace(userName))
455+
{
456+
client.Authenticate(new SaslMechanismNtlm(userName, password));
457+
}
458+
else
459+
{
460+
client.Authenticate(new SaslMechanismNtlm(CredentialCache.DefaultNetworkCredentials));
461+
}
462+
}
449463

450464
client.Send(message);
451465
InternalLogger.Trace("{0}: Sending mail done. Disconnecting", this);
@@ -535,17 +549,12 @@ private void CheckRequiredParameters()
535549
throw new NLogConfigurationException("MailTarget - To address is required");
536550
}
537551

538-
var smtpAuthentication = RenderLogEvent(SmtpAuthentication, LogEventInfo.CreateNullEvent());
539-
if (smtpAuthentication == SmtpAuthenticationMode.Ntlm)
540-
{
541-
throw new NLogConfigurationException("MailTarget - SmtpAuthentication NTLM not yet supported");
542-
}
543-
544552
if (IsEmptyLayout(PickupDirectoryLocation) && IsEmptyLayout(SmtpServer))
545553
{
546554
throw new NLogConfigurationException("MailTarget - SmtpServer is required");
547555
}
548556

557+
var smtpAuthentication = RenderLogEvent(SmtpAuthentication, LogEventInfo.CreateNullEvent());
549558
if (smtpAuthentication == SmtpAuthenticationMode.OAuth2 && (IsEmptyLayout(SmtpUserName) || IsEmptyLayout(SmtpPassword)))
550559
{
551560
throw new NLogConfigurationException("MailTarget - SmtpUserName (OAuth UserName) and SmtpPassword (OAuth AccessToken) is required when SmtpAuthentication = OAuth2");

test/NLog.MailKit.Tests/UnitTests/MailTargetTests.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -189,27 +189,6 @@ public void MailTargetInitialize_WithoutSpecifiedSmtpServer_ThrowsConfigExceptio
189189
);
190190
}
191191

192-
[Fact]
193-
public void MailTargetInitialize_WithSmtpAuthenticationModeNtlm_ThrowsConfigException()
194-
{
195-
var mmt = new MailTarget
196-
{
197-
From = "foo@bar.com",
198-
To = "bar@bar.com",
199-
Subject = "Hello from NLog",
200-
SmtpServer = "server1",
201-
SmtpPort = 27,
202-
SmtpAuthentication = SmtpAuthenticationMode.Ntlm,
203-
};
204-
205-
Assert.Throws<NLogConfigurationException>(() =>
206-
new LogFactory().Setup().LoadConfiguration(cfg =>
207-
{
208-
cfg.Configuration.AddRuleForAllLevels(mmt);
209-
})
210-
);
211-
}
212-
213192
[Fact]
214193
public void MailTargetInitialize_WithSmtpAuthenticationModeOAuth2_ThrowsConfigException()
215194
{

0 commit comments

Comments
 (0)