A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/NLog/NLog/wiki/Mail-Target below:

Mail target · NLog/NLog Wiki · GitHub

Sends log messages by email using SMTP protocol.

Platforms Supported: All - Requires nuget-package NLog.Targets.Mail since NLog v6, or alternative MailKit

Combines well with FallbackGroup Target in order to create a fallback with multiple SMTP Hosts, example see here.

<targets>
  <target xsi:type="Mail"
          name="String"
          header="Layout"
          footer="Layout"
          layout="Layout"
          html="Boolean"
          addNewLines="Boolean"
          replaceNewlineWithBrTagInHtml="Boolean"
          encoding="Encoding"
          subject="Layout"
          to="Layout"
          bcc="Layout"
          cc="Layout"
          from="Layout"
          body="Layout"
          smtpUserName="Layout"
          enableSsl="Boolean"
          secureSocketOption="None|Auto|SslOnConnect|StartTls|StartTlsWhenAvailable"*
          smtpPassword="Layout"
          smtpAuthentication="Enum"
          smtpServer="Layout"
          smtpPort="Integer"
          useSystemNetMailSettings="Boolean"
          deliveryMethod="Enum"
          pickupDirectoryLocation="String"
          timeout="Integer"
          skipCertificateValidation="Boolean"
          priority="Layout">
     <mailheader name="header-name" layout="header-value" /> <!-- repeatable -->
 </target>
</targets>

Read more about using the Configuration File.

.NET Standard 1.x / MailKit package

The SmtpClient is not part of .NET Standard 1.x. Also notice that the original SmtpClient of .NET is obsolete in favor of MailKit:

The NLog.MailKit is the Mail target implementation which uses MailKit. Including the NLog.MailKit package will replace the original mail target and has the same options as the original mail target

or use

<PackageReference Include="NLog.MailKit" Version="3.0.0" />

And add to your nlog.config:

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

Bug/feature requests for NLog.MailKit please on the NLog.MailKit repo

Application Configuration File

If the application config file contains mail settings, fx.:

<system.net>
  <mailSettings>
    <smtp from="mail@domain.com" deliveryMethod="SpecifiedPickupDirectory">
      <network host="localhost" port="25"/>
      <specifiedPickupDirectory pickupDirectoryLocation="C:/Temp/Email"/>
    </smtp>
  </mailSettings>
</system.net>

These values will be used, if target doesn't override them (see useSystemNetMailSettings attribute).

It is possible to use an address in format "John Doe <john.doe@example.com>" but the special characters < and > must be escaped. The result would be John Doe &lt;john.doe@example.com&gt;

MailKit Target using Amazon AWS SMTP:
<target name="MailAWS" xsi:type="Mail"
            smtpServer="email-smtp.us-east-1.amazonaws.com"
            smtpPort="587"
            smtpAuthentication="Basic"
            smtpUserName="xxxxxxxx"
            smtpPassword="xxxxxxxxxx"
            secureSocketOption="StartTls"
            from="logger@xxxx.xxx" to="xxx@gmail.com" />

Example configuration for a Mailserver Fallback with multiple hosts.

<target xsi:type="FallbackGroup" 
        name="mail"
        returnToFirstOnSuccess="true">
    <target xsi:type="Mail"
            name="mailserver1"
            subject="Layout"
            to="Layout"
            from="Layout"
            smtpServer="mx1.example.com" 
            smtpPort="Integer"
            layout="Layout" />
    <target xsi:type="Mail"
            name="mailserver2" 
            subject="Layout"
            to="Layout"
            from="Layout"
            smtpServer="mx2.example.com" 
            smtpPort="Integer"
            layout="Layout" />
</target>
Mail Target send LogEvent history on error

Example configuration that buffers LogEvents and sends entire batch on error LogEvent:

  <target xsi:type="AutoFlushWrapper" name="MailErrorFlush"
    condition="level >= LogLevel.Error"
    flushOnConditionOnly="true">
     <target xsi:type="BufferingWrapper" name="MailBuffer"
       bufferSize="50"
       overflowAction="Discard">
        <target xsi:type="Mail" name="MailTarget" />
     </target>
  </target>
Using Mail-Target with GMail
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">

  <targets>
    <target name="gmail" xsi:type="Mail"
            smtpServer="smtp.gmail.com"
            smtpPort="587"
            smtpAuthentication="Basic"
            smtpUserName="user@gmail.com"
            smtpPassword="password"
            enableSsl="true"
            from="emailaddress@gmail.com"
            to="recipient@example.com"
            cc="alice@example.com;bob@example.com;charlie@example.com"
          />
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="gmail" />
  </rules>
</nlog>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4