Skip to content
Merged
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
51 changes: 29 additions & 22 deletions src/NLog.MailKit/MailTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,22 +416,9 @@ private void SendMailMessage(MimeMessage message, LogEventInfo lastEvent)
client.Connect(renderedHost, smtpPort, secureSocketOptions);
InternalLogger.Trace("{0}: Connecting succesfull with SmtpCapabilities={1}", this, client.Capabilities);

// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");

// Note: only needed if the SMTP server requires authentication

var smtpAuthentication = RenderLogEvent(SmtpAuthentication, lastEvent);
if (smtpAuthentication == SmtpAuthenticationMode.Basic)
{
var userName = RenderLogEvent(SmtpUserName, lastEvent);
var password = RenderLogEvent(SmtpPassword, lastEvent);

InternalLogger.Trace("{0}: Authenticate with username '{1}'", this, userName);
client.Authenticate(userName, password);
}
else if (smtpAuthentication == SmtpAuthenticationMode.OAuth2)
if (smtpAuthentication == SmtpAuthenticationMode.OAuth2)
{
var userName = RenderLogEvent(SmtpUserName, lastEvent);
var oauth2Token = RenderLogEvent(SmtpPassword, lastEvent);
Expand All @@ -444,20 +431,40 @@ private void SendMailMessage(MimeMessage message, LogEventInfo lastEvent)
throw new NLogRuntimeException(string.Format(RequiredPropertyIsEmptyFormat, nameof(SmtpUserName)));
}
InternalLogger.Trace("{0}: Authenticate with OAuth2 username '{1}'", this, userName);
var oauth2 = new SaslMechanismOAuth2(userName, oauth2Token);

SaslMechanism oauth2 = client.AuthenticationMechanisms.Contains("OAUTHBEARER") ?
new SaslMechanismOAuthBearer(userName, oauth2Token) :
new SaslMechanismOAuth2(userName, oauth2Token);
client.Authenticate(oauth2);
}
else if (smtpAuthentication == SmtpAuthenticationMode.Ntlm)
else
{
var userName = RenderLogEvent(SmtpUserName, lastEvent);
var password = RenderLogEvent(SmtpPassword, lastEvent);
if (!string.IsNullOrWhiteSpace(userName))
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.AuthenticationMechanisms.Remove("OAUTHBEARER");

if (smtpAuthentication == SmtpAuthenticationMode.Basic)
{
client.Authenticate(new SaslMechanismNtlm(userName, password));
var userName = RenderLogEvent(SmtpUserName, lastEvent);
var password = RenderLogEvent(SmtpPassword, lastEvent);

InternalLogger.Trace("{0}: Authenticate with username '{1}'", this, userName);
client.Authenticate(userName, password);
}
else
else if (smtpAuthentication == SmtpAuthenticationMode.Ntlm)
{
client.Authenticate(new SaslMechanismNtlm(CredentialCache.DefaultNetworkCredentials));
var userName = RenderLogEvent(SmtpUserName, lastEvent);
var password = RenderLogEvent(SmtpPassword, lastEvent);
if (!string.IsNullOrWhiteSpace(userName))
{
client.Authenticate(new SaslMechanismNtlm(userName, password));
}
else
{
// Default NTLM credentials probably only works on Windows
client.Authenticate(new SaslMechanismNtlm(CredentialCache.DefaultNetworkCredentials));
}
}
}

Expand Down
Loading