[This is preliminary documentation and is subject to change.]

Sends log messages by email using SMTP protocol.

Namespace:  NLog.Targets
Assembly:  NLog (in NLog.dll) Version: 2.0.1.0

Syntax

C#
public class MailTarget : TargetWithLayoutHeaderAndFooter
Visual Basic (Declaration)
Public Class MailTarget _
	Inherits TargetWithLayoutHeaderAndFooter
Visual Basic (Usage)
Dim instance As MailTarget

Examples

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

CopyXML
 1<?xml version="1.0" ?>
 2<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
 3      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 4
 5    <targets>
 6        <target name="mail" xsi:type="Mail"
 7            smtpServer="192.168.0.15" 
 8            from="jaak@jkowalski.net"
 9            to="jaak@jkowalski.net"
10            subject="test subject" />
11    </targets>
12
13    <rules>
14        <logger name="*" minlevel="Debug" writeTo="mail" />
15    </rules>
16</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:

CopyC#
 1using System;
 2
 3using NLog;
 4using NLog.Targets;
 5
 6class Example
 7{
 8    static void Main(string[] args)
 9    {
10        try
11        {
12            Console.WriteLine("Setting up the target...");
13            MailTarget target = new MailTarget();
14
15            target.SmtpServer = "192.168.0.15";
16            target.From = "jaak@jkowalski.net";
17            target.To = "jaak@jkowalski.net";
18            target.Subject = "sample subject";
19
20            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
21
22            Console.WriteLine("Sending...");
23            Logger logger = LogManager.GetLogger("Example");
24            Console.WriteLine("Sent.");
25            logger.Debug("log message");
26        }
27        catch (Exception ex)
28        {
29            Console.WriteLine("EX: {0}", ex);
30
31        }
32    }
33}

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

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

CopyXML
 1<?xml version="1.0" ?>
 2<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
 3      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 4
 5    <targets>
 6        <target name="mail" xsi:type="BufferingWrapper" bufferSize="5">
 7            <target xsi:type="Mail"
 8                smtpServer="192.168.0.15" 
 9                from="jaak@jkowalski.net"
10                to="jaak@jkowalski.net"
11                subject="test subject" />
12        </target>
13    </targets>
14
15    <rules>
16        <logger name="*" minlevel="Debug" writeTo="mail" />
17    </rules>
18</nlog>

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

CopyC#
 1using System;
 2
 3using NLog;
 4using NLog.Targets;
 5using NLog.Targets.Wrappers;
 6
 7class Example
 8{
 9    static void Main(string[] args)
10    {
11        try
12        {
13            NLog.Internal.InternalLogger.LogToConsole = true;
14            NLog.Internal.InternalLogger.LogLevel = LogLevel.Trace;
15            Console.WriteLine("Setting up the target...");
16            MailTarget target = new MailTarget();
17
18            target.SmtpServer = "192.168.0.15";
19            target.From = "jaak@jkowalski.net";
20            target.To = "jaak@jkowalski.net";
21            target.Subject = "sample subject";
22            target.Body = "${message}${newline}";
23
24            BufferingTargetWrapper buffer = new BufferingTargetWrapper(target, 5);
25
26            NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(buffer, LogLevel.Debug);
27
28            Console.WriteLine("Sending...");
29            Logger logger = LogManager.GetLogger("Example");
30            logger.Debug("log message 1");
31            logger.Debug("log message 2");
32            logger.Debug("log message 3");
33            logger.Debug("log message 4");
34            logger.Debug("log message 5");
35            logger.Debug("log message 6");
36            logger.Debug("log message 7");
37            logger.Debug("log message 8");
38
39            // this should send 2 mails - one with messages 1..5, the other with messages 6..8
40            Console.WriteLine("Sent.");
41        }
42        catch (Exception ex)
43        {
44            Console.WriteLine("EX: {0}", ex);
45
46        }
47    }
48}

Inheritance Hierarchy

Object
  NLog.Targets..::.Target
    NLog.Targets..::.TargetWithLayout
      NLog.Targets..::.TargetWithLayoutHeaderAndFooter
        NLog.Targets..::.MailTarget

See Also