title: Trying to send email via Nlog via gmail
body: <p>I have an application written with c# and i'm trying to use Nlog to handle logging and email notification.  I have no problem with logging but I seem to have a big problem with email handling.</p>
  
  <p>Following their recommendations in the wiki, I installed Nlog Mailkit and configured it according to directions, added targets and rules.  This is what my config file looks like now:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;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"
        autoReload="true"
        throwExceptions="false"
        internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"&gt;
  
    &lt;extensions&gt;
      &lt;add assembly="NLog.MailKit"/&gt;
  &lt;/extensions&gt;
  
    &lt;variable name="myvar" value="myvalue"/&gt;
  
    &lt;targets&gt;
      &lt;target 
        xsi:type="Mail" 
        name="errMail" 
        subject="test" 
        to="me@domain.com" 
        from="my_acct@gmail.com" 
        smtpUserName="my_acct@gmail.com" 
        enableSsl="true" 
        smtpPassword="pwd" 
        smtpAuthentication="Basic" 
        smtpServer="smtp.gmail.com" 
        smtpPort="587" 
      /&gt;
    &lt;/targets&gt;
  
    &lt;rules&gt;
      &lt;!-- add your logging rules here --&gt;
      &lt;logger name="*" level="Error" writeTo="errMail" /&gt;
    &lt;/rules&gt;
  &lt;/nlog&gt;
  </code></pre>
  
  <p>and as a simple test, this is my c# code:</p>
  
  <pre><code>namespace ConsoleApp1
  {
      class Program
      {
          private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
  
          static void Main(string[] args)
          {
              try
              {
                  Logger.Error("error test");
              }
              catch (Exception ex)
              {
  
              }
          }
      }
  }
  </code></pre>
  
  <p>And when I run this, nothing seems to happen... no email, nothing.  If I swap the target with a file, I get an log file, so I know it generally works.</p>
  
  <p>As another test, I dropped in some simple code to do email directly:</p>
  
  <pre><code>var fromAddress = new MailAddress("my_acct@gmail.com", "From Name");
  var toAddress = new MailAddress("me@domain.com", "To Name");
  const string fromPassword = "pwd";
  const string subject = "Subject";
  const string body = "Body";
  
  var smtp = new SmtpClient
  {
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
  };
  
  using (var message = new MailMessage(fromAddress, toAddress)
  {
    Subject = subject,
    Body = body
  })
  
  {
    smtp.Send(message);
  }
  </code></pre>
  
  <p>The smtp test works fine, so in theory there's no problem reaching gmail or sending email via gmail from my desktop.... so I can only assume there must be something I'm doing wrong either in the configuration for nlog or the call... but I can't figure out what.</p>
  
  <p>I was originally trying to send via my companies smtp server but thought maybe there were some access issues preventing me from doing so so I swapped to gmail thinking it would be simpler.</p>
  
  <p>I've done all the research I can and I'm pulling my hair out as to what the problem could be... HELP!!!</p>