Alternative Mail target for NLog using MailKit. Compatible with .NET standard 2+
The NLog MailKit Target is able to replace the original NLog Mail Target and have the same options. See docs for NLog Mail Target.
But Mailkit does not yet support SmtpAuthentication = NTLM.
- 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.
This library is integration tested with the SmtpServer NuGet package
-
Install the package:
Install-Package NLog.MailKitor in your csproj:<PackageReference Include="NLog.MailKit" Version="6.*" />
-
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>());
See the NLog Wiki for available options and examples.
Note that the option skipCertificateValidation="true" can prevent AuthenticationException if your remote certificate for smtpServer is invalid - not recommend!
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.
BSD. License of MailKit is MIT