Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/NLog.MailKit/MailTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down
21 changes: 0 additions & 21 deletions test/NLog.MailKit.Tests/UnitTests/MailTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NLogConfigurationException>(() =>
new LogFactory().Setup().LoadConfiguration(cfg =>
{
cfg.Configuration.AddRuleForAllLevels(mmt);
})
);
}

[Fact]
public void MailTargetInitialize_WithSmtpAuthenticationModeOAuth2_ThrowsConfigException()
{
Expand Down
Loading