3

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.

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.

Thanks.

Here is the C# code I use for sending an email:

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 ())
}
UltraJ
  • 467
  • 1
  • 10
  • 20
  • Is this Task Scheduler on a server? – m.casey Jul 16 '14 at 16:51
  • How do you send the e-mail? Talking to exchange, smtp, or com-interop on outlook? – Styxxy Jul 16 '14 at 16:51
  • Yes. The task scheduler is being run on a Windows 2008 R2 server. I've added the C# code to the question which shows how the email is being sent. This code works on the command line on the server, but not when it is run as a scheduled task. – UltraJ Jul 16 '14 at 17:05
  • Given your code, I'd say it might be time to catch that exception and log it out to the server or a text file. You're more than likely throwing an exception of some sort and this would make troubleshooting much easier. – m.casey Jul 17 '14 at 00:47
  • Check this https://stackoverflow.com/a/60457227/2629117 – Ishara Samintha Oct 03 '22 at 13:42

1 Answers1

0

Assuming you're running the console application via Task Scheduler, one can change the user which runs the scheduler to one which has permission to send email. If you're using DefaultCredentials = true in your code, then this is especially important.

Typically this is done by creating a service account with sufficient permissions to perform the task at hand and nothing more. One can configure which user account will execute the task on the General tab of the task in question within the Task Scheduler. There will be a Change User button. Be sure to check "Run whether the user is logged on or not".

m.casey
  • 2,579
  • 17
  • 10
  • Hi m.casey. I've been trying different user types here but with no success in sending an email. How do you give a user permission to send mail? You would think that an administrator could send without restrictions. – UltraJ Jul 16 '14 at 19:43
  • That depends greatly upon one's environment. In a Windows environment, for instance, one would need access to the SMTP server in question (assuming we're using Windows authentication style credentials) and [logon as batch rights](http://technet.microsoft.com/en-us/library/cc957131.aspx). – m.casey Jul 16 '14 at 19:53
  • If your in a corporate environment, you might need what is sometimes called a "smarthost". – Burt_Harris Jul 17 '14 at 04:28