@@ -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 ) ;
@@ -444,20 +431,40 @@ private void SendMailMessage(MimeMessage message, LogEventInfo lastEvent)
444431 throw new NLogRuntimeException ( string . Format ( RequiredPropertyIsEmptyFormat , nameof ( SmtpUserName ) ) ) ;
445432 }
446433 InternalLogger . Trace ( "{0}: Authenticate with OAuth2 username '{1}'" , this , userName ) ;
447- var oauth2 = new SaslMechanismOAuth2 ( userName , oauth2Token ) ;
434+
435+ SaslMechanism oauth2 = client . AuthenticationMechanisms . Contains ( "OAUTHBEARER" ) ?
436+ new SaslMechanismOAuthBearer ( userName , oauth2Token ) :
437+ new SaslMechanismOAuth2 ( userName , oauth2Token ) ;
448438 client . Authenticate ( oauth2 ) ;
449439 }
450- else if ( smtpAuthentication == SmtpAuthenticationMode . Ntlm )
440+ else
451441 {
452- var userName = RenderLogEvent ( SmtpUserName , lastEvent ) ;
453- var password = RenderLogEvent ( SmtpPassword , lastEvent ) ;
454- if ( ! string . IsNullOrWhiteSpace ( userName ) )
442+ // Note: since we don't have an OAuth2 token, disable
443+ // the XOAUTH2 authentication mechanism.
444+ client . AuthenticationMechanisms . Remove ( "XOAUTH2" ) ;
445+ client . AuthenticationMechanisms . Remove ( "OAUTHBEARER" ) ;
446+
447+ if ( smtpAuthentication == SmtpAuthenticationMode . Basic )
455448 {
456- client . Authenticate ( new SaslMechanismNtlm ( userName , password ) ) ;
449+ var userName = RenderLogEvent ( SmtpUserName , lastEvent ) ;
450+ var password = RenderLogEvent ( SmtpPassword , lastEvent ) ;
451+
452+ InternalLogger . Trace ( "{0}: Authenticate with username '{1}'" , this , userName ) ;
453+ client . Authenticate ( userName , password ) ;
457454 }
458- else
455+ else if ( smtpAuthentication == SmtpAuthenticationMode . Ntlm )
459456 {
460- client . Authenticate ( new SaslMechanismNtlm ( CredentialCache . DefaultNetworkCredentials ) ) ;
457+ var userName = RenderLogEvent ( SmtpUserName , lastEvent ) ;
458+ var password = RenderLogEvent ( SmtpPassword , lastEvent ) ;
459+ if ( ! string . IsNullOrWhiteSpace ( userName ) )
460+ {
461+ client . Authenticate ( new SaslMechanismNtlm ( userName , password ) ) ;
462+ }
463+ else
464+ {
465+ // Default NTLM credentials probably only works on Windows
466+ client . Authenticate ( new SaslMechanismNtlm ( CredentialCache . DefaultNetworkCredentials ) ) ;
467+ }
461468 }
462469 }
463470
0 commit comments