Skip to content

Commit a1db66e

Browse files
committed
Support SmtpAuthenticationMode.Ntlm
1 parent 549c04b commit a1db66e

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
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");

0 commit comments

Comments
 (0)