Skip to content

Commit cb261a7

Browse files
committed
Skip removing XOAUTH2 from AuthenticationMechanisms when OAuth2
1 parent 0f5ecf2 commit cb261a7

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

src/NLog.MailKit/MailTarget.cs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -416,22 +416,9 @@ private void SendMailMessage(MimeMessage message, LogEventInfo lastEvent)
416416
client.Connect(renderedHost, smtpPort, secureSocketOptions);
417417
InternalLogger.Trace("{0}: Connecting succesfull with SmtpCapabilities={1}", this, client.Capabilities);
418418

419-
// Note: since we don't have an OAuth2 token, disable
420-
// the XOAUTH2 authentication mechanism.
421-
client.AuthenticationMechanisms.Remove("XOAUTH2");
422-
423419
// Note: only needed if the SMTP server requires authentication
424-
425420
var smtpAuthentication = RenderLogEvent(SmtpAuthentication, lastEvent);
426-
if (smtpAuthentication == SmtpAuthenticationMode.Basic)
427-
{
428-
var userName = RenderLogEvent(SmtpUserName, lastEvent);
429-
var password = RenderLogEvent(SmtpPassword, lastEvent);
430-
431-
InternalLogger.Trace("{0}: Authenticate with username '{1}'", this, userName);
432-
client.Authenticate(userName, password);
433-
}
434-
else if (smtpAuthentication == SmtpAuthenticationMode.OAuth2)
421+
if (smtpAuthentication == SmtpAuthenticationMode.OAuth2)
435422
{
436423
var userName = RenderLogEvent(SmtpUserName, lastEvent);
437424
var oauth2Token = RenderLogEvent(SmtpPassword, lastEvent);
@@ -447,17 +434,32 @@ private void SendMailMessage(MimeMessage message, LogEventInfo lastEvent)
447434
var oauth2 = new SaslMechanismOAuth2(userName, oauth2Token);
448435
client.Authenticate(oauth2);
449436
}
450-
else if (smtpAuthentication == SmtpAuthenticationMode.Ntlm)
437+
else
451438
{
452-
var userName = RenderLogEvent(SmtpUserName, lastEvent);
453-
var password = RenderLogEvent(SmtpPassword, lastEvent);
454-
if (!string.IsNullOrWhiteSpace(userName))
439+
// Note: since we don't have an OAuth2 token, disable
440+
// the XOAUTH2 authentication mechanism.
441+
client.AuthenticationMechanisms.Remove("XOAUTH2");
442+
443+
if (smtpAuthentication == SmtpAuthenticationMode.Basic)
455444
{
456-
client.Authenticate(new SaslMechanismNtlm(userName, password));
445+
var userName = RenderLogEvent(SmtpUserName, lastEvent);
446+
var password = RenderLogEvent(SmtpPassword, lastEvent);
447+
448+
InternalLogger.Trace("{0}: Authenticate with username '{1}'", this, userName);
449+
client.Authenticate(userName, password);
457450
}
458-
else
451+
else if (smtpAuthentication == SmtpAuthenticationMode.Ntlm)
459452
{
460-
client.Authenticate(new SaslMechanismNtlm(CredentialCache.DefaultNetworkCredentials));
453+
var userName = RenderLogEvent(SmtpUserName, lastEvent);
454+
var password = RenderLogEvent(SmtpPassword, lastEvent);
455+
if (!string.IsNullOrWhiteSpace(userName))
456+
{
457+
client.Authenticate(new SaslMechanismNtlm(userName, password));
458+
}
459+
else
460+
{
461+
client.Authenticate(new SaslMechanismNtlm(CredentialCache.DefaultNetworkCredentials));
462+
}
461463
}
462464
}
463465

0 commit comments

Comments
 (0)