Skip to content

NLog/NLog.MailKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

203 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NLog.MailKit

NuGet Build Status Bugs Vulnerabilities Coverage

Alternative Mail target for NLog using MailKit.

The NLog MailKit Target is able to replace the original NLog Mail Target and provides the same options.

See the NLog Wiki for available options and examples.

  • Can also be combined with FallbackGroup to fallback to alternative SMTP-server on error.
  • Can also be combined with BufferingWrapper to group multiple logevents into a single email.
  • Can also be combined with LimitingWrapper to throttle the number of emails.
  • Can also be combined with FilteringWrapper to avoid repeating identical emails.

If having trouble with output, then check NLog InternalLogger for clues. See also Troubleshooting NLog

This library is integration tested with the SmtpServer NuGet package

How to use

  1. Install the package:

    Install-Package NLog.MailKit or in your csproj:

    <PackageReference Include="NLog.MailKit" Version="6.*" />
  2. Add to your nlog.config:

    <extensions>
        <add assembly="NLog.MailKit"/>
    </extensions>

    Alternative register from code using fluent configuration API:

    LogManager.Setup().SetupExtensions(ext => ext.RegisterTarget<NLog.MailKit.MailTarget>());

Note that the option skipCertificateValidation="true" can prevent AuthenticationException if your remote certificate for smtpServer is invalid - not recommend!

OAuth2 Authentication

Mailkit supports OAuth2 authentication by specifying SmtpAuthentication = OAuth2 together with:

  • SmtpUserName = ${gdc:OAuthClientId}
  • SmtpPassword = ${gdc:OAuthClientSecret}

Before using OAuth2 authentication, make sure to acquire an access token from your email provider (e.g., Gmail, Outlook). This can be implemented with a Custom NLog Layout Renderer, or by storing the details in environment variable or NLog Global Diagnostics Context (GDC) and point SmtpUserName and SmtpPassword to these locations.

License

BSD. License of MailKit is MIT