title: Command Line App Can't Send Email Through Scheduled Tasks
body: <p>I wrote a small command line utility in C# that sends an email as a result of actions that it takes. The email is sent successfully when it is run manually from the command line, but it doesn't send when its run as a scheduled task. I set it to run with the highest user settings as a scheduled task. There are no firewall settings that would block outgoing emails on the test machine.</p>
  
  <p>Do scheduled tasks run as a user that is restricted from sending emails? I'm not sure if there is any code for sending emails in C# that can set the mailer as an administrator.</p>
  
  <p>Thanks.</p>
  
  <p>Here is the C# code I use for sending an email:</p>
  
  <pre><code>public static void SendNotifications ()
  {
    string smtpServer = "mailserver.com";
    string smtpUser = "a@b.com";
    string smtpPassword = "abc123";
  
    // Set the variables for the mail object.
  
    using (MailMessage Email = new MailMessage ())
    {
      Email.IsBodyHtml = true;
  
      Email.From = new MailAddress (smtpUser);
  
      Email.To.Add ("a1@b.com");
      Email.CC.Add ("a2@b.com");
  
      Email.Subject = "Subject";
  
      Email.Body = @"Here is a notification.";
  
      SmtpClient smtp = new SmtpClient (smtpServer);
  
      System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential (smtpUser, smtpPassword);
  
      smtp.UseDefaultCredentials = false;
      smtp.Credentials = SMTPUserInfo;
  
      try
      {
        // Send the mail.
  
        smtp.Send (Email);
      } // try
  
      catch (Exception ex)
      {
        // ignore error message
      } // catch (Exception e)
    } // using (MailMessage Email = new MailMessage ())
  }
  </code></pre>