Mail Target


Sends logging messages by email using SMTP protocol.

AssemblyClass.NET Framework.NET CFMono on WindowsMono on Unix
1.01.12.01.02.01.02.01.02.0
NLog.dllNLog.Targets.MailTarget  

Parameters (blue fields are required):

NameTypeDescription
namestring
The name of the target.
addNewLinesboolean
Whether to add new lines between log entries.
bccstring
BCC email addresses separated by semicolons (e.g. john@domain.com;jane@domain.com)
bodystring
Mail message body (repeated for each log message send in one mail)

Default value is: ${message}.

ccstring
CC email addresses separated by semicolons (e.g. john@domain.com;jane@domain.com)
encodingstring
Encoding to be used for sending e-mail.

Default value is: UTF8.

footerstring  ${}
Footer
fromstring
Sender's email address (e.g. joe@domain.com)
headerstring  ${}
Header
htmlboolean
Send message as HTML instead of plain text.

Default value is: False.

smtpAuthenticationSmtpAuthenticationMode
SMTP Authentication mode.

Default value is: None.

Possible values are:

  • None - No authentication.
  • Basic - Basic - username and password
  • Ntlm - NTLM Authentication

smtpPasswordstring
The password used to authenticate against SMTP server (used when SmtpAuthentication is set to "basic").
smtpPortinteger
The port that SMTP Server is listening on.

Default value is: 25.

smtpServerstring
SMTP Server to be used for sending.
smtpUsernamestring
The username used to connect to SMTP server (used when SmtpAuthentication is set to "basic").
subjectstring
Mail subject.

Default value is: Message from NLog on ${machinename}.

tostring
Recipients' email addresses separated by semicolons (e.g. john@domain.com;jane@domain.com)

Example:

To set up the target in the configuration file, use the following syntax:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <targets> 
    <target name="mail" xsi:type="Mail" smtpServer="192.168.0.15" 
            from="jaak@jkowalski.net" to="jaak@jkowalski.net" 
            subject="test subject"/> 
  </targets> 
  <rules> 
    <logger name="*" minlevel="Debug" writeTo="mail"/> 
  </rules> 
</nlog> 

This assumes just one target and a single rule. More configuration options are described here.

To set up the log target programmatically use code like this:

using System; 
 
using NLog; 
using NLog.Targets; 
 
class Example 
{ 
    static void Main(string[] args) 
    { 
        try 
        { 
            Console.WriteLine("Setting up the target..."); 
            MailTarget target = new MailTarget(); 
 
            target.SmtpServer = "192.168.0.15"; 
            target.From = "jaak@jkowalski.net"; 
            target.To = "jaak@jkowalski.net"; 
            target.Subject = "sample subject"; 
 
            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug); 
 
            Console.WriteLine("Sending..."); 
            Logger logger = LogManager.GetLogger("Example"); 
            Console.WriteLine("Sent."); 
            logger.Debug("log message"); 
        } 
        catch (Exception ex) 
        { 
            Console.WriteLine("EX: {0}", ex); 
                 
        } 
    } 
}

Mail target works best when used with BufferingWrapper target which lets you send multiple logging messages in single mail

To set up the buffered mail target in the configuration file, use the following syntax:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
  <targets> 
    <target name="mail" xsi:type="BufferingWrapper" bufferSize="5"> 
      <target xsi:type="Mail" smtpServer="192.168.0.15" 
              from="jaak@jkowalski.net" to="jaak@jkowalski.net" 
              subject="test subject"/> 
    </target> 
  </targets> 
  <rules> 
    <logger name="*" minlevel="Debug" writeTo="mail"/> 
  </rules> 
</nlog> 

To set up the buffered mail target programmatically use code like this:

using System; 
 
using NLog; 
using NLog.Targets; 
using NLog.Targets.Wrappers; 
 
class Example 
{ 
    static void Main(string[] args) 
    { 
        try 
        { 
            NLog.Internal.InternalLogger.LogToConsole = true; 
            NLog.Internal.InternalLogger.LogLevel = LogLevel.Trace; 
            Console.WriteLine("Setting up the target..."); 
            MailTarget target = new MailTarget(); 
 
            target.SmtpServer = "192.168.0.15"; 
            target.From = "jaak@jkowalski.net"; 
            target.To = "jaak@jkowalski.net"; 
            target.Subject = "sample subject"; 
            target.Body = "${message}${newline}"; 
 
            BufferingTargetWrapper buffer = new BufferingTargetWrapper(target, 5); 
 
            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(buffer, LogLevel.Debug); 
 
            Console.WriteLine("Sending..."); 
            Logger logger = LogManager.GetLogger("Example"); 
            logger.Debug("log message 1"); 
            logger.Debug("log message 2"); 
            logger.Debug("log message 3"); 
            logger.Debug("log message 4"); 
            logger.Debug("log message 5"); 
            logger.Debug("log message 6"); 
            logger.Debug("log message 7"); 
            logger.Debug("log message 8"); 
 
            // this should send 2 mails - one with messages 1..5, the other with messages 6..8 
            Console.WriteLine("Sent."); 
        } 
        catch (Exception ex) 
        { 
            Console.WriteLine("EX: {0}", ex); 
                 
        } 
    } 
}

Back to the target list.