Resolved How can we schedule a C# .exe file ?

Anonymous

Well-known member
Joined
Sep 29, 2020
Messages
84
Programming Experience
Beginner
Hi,

I had developed a console application which accepts two user defined inputs. The user double clicks the .exe, enters the input, and the application runs accordingly. Now, a request has come to me to schedule that .exe to run at the start of every month. I have already checked Windows Scheduler but that accepts only one argument whereas my application accepts two arguments.
Could you suggest me how can I achieve this objective?
Thanks!
 
Windows Scheduler but that accepts only one argument
That is not true. Any number of arguments separated by space can be given.
 
This is not a VS.NET question. This is a generic Windows OS question. Going to try to find a better subforum.
 
Any number of arguments separated by space can be given.
Hello, I am sorry I am back on this question. Thank you this works.

But now the end user wants me to schedule the .exe and after the completion of task( my .exe updates the database), the users wants to receive an email that the database has been updated successfully.

I checked if we can do this using Task Scheduler but the email option says its deprecated. So, is there any other way I can send the alert email or I would only have to code it in Visual Studio?

Thank you for your guidance!
 
You could have your program send the email as well.

Or you could setup a scheduled task that invokes PowerShell to send email, but I don't think there is a way to make one task conditional on the success of another task. You would have to write in the PowerShell script a check for successful run of your console app.

Sounnds like too many moving parts with the two separate tasks. Better to modify your code to send email.
 
You could have your program send the email as well.

Or you could setup a scheduled task that invokes PowerShell to send email, but I don't think there is a way to make one task conditional on the success of another task. You would have to write in the PowerShell script a check for successful run of your console app.

Sounnds like too many moving parts with the two separate tasks. Better to modify your code to send email.
I modified my code and used smtp to send email. But in the code i had to specify the credentials of the sender's email address. I used the following code -
C#:
protected void SendEmail(string emailAddres)
{
    SmtpClient smtp = new SmtpClient();

    MailMessage mail = new MailMessage()
    {
        To = { new MailAddress(emailAddres) },
        From = new MailAddress("fromEmail"),
        Subject = "Subject of a test email!",
        IsBodyHtml = true,
        Body = "Test body email.",
        BodyEncoding = System.Text.Encoding.UTF8,
        SubjectEncoding = System.Text.Encoding.UTF8
    };

   try
   {
       smtp.Send(mail);

}


App config :

C#:
<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network">
        <network host="smtp.office365.com" port="587" defaultCredentials="false" enableSsl="true"
                 userName="email@email.com" password="ThisPwd" targetName="STARTTLS/smtp.office365.com"
        />
      </smtp>
    </mailSettings>
  </system.net>


Is there a way to send emails without having to specify the credentials of the sender's email ?
 
If you can find an SMTP relay that doesn't take credentials, yes. Mostly, its spammers who use such relays.

As an aside, you are not specifying the credentials in your code. You have the credentials in your configuration file. This is good in that you can update the credentials without having to recompile. This is bad in that the password is in out in the open. You can also set the credentials on the SmtpClient object at runtime instead of depending on the configuration file. The question is where will you stash the credentials? Personally, I would recommend keeping them in the configuration file, but encrypted using DPAPI. That way even if someone copies the file, the encryption will only work on your machine for your user. They can't use it on the same machine with a different login user, or on a different machine with the same user. You could also try some obfuscation by stashing the encrypted credentials elsewhere (like in the registry) as a defense in depth, but obfuscation is not true security. Encryption is.
 
If you can find an SMTP relay that doesn't take credentials, yes. Mostly, its spammers who use such relays.

As an aside, you are not specifying the credentials in your code. You have the credentials in your configuration file. This is good in that you can update the credentials without having to recompile. This is bad in that the password is in out in the open. You can also set the credentials on the SmtpClient object at runtime instead of depending on the configuration file. The question is where will you stash the credentials? Personally, I would recommend keeping them in the configuration file, but encrypted using DPAPI. That way even if someone copies the file, the encryption will only work on your machine for your user. They can't use it on the same machine with a different login user, or on a different machine with the same user. You could also try some obfuscation by stashing the encrypted credentials elsewhere (like in the registry) as a defense in depth, but obfuscation is not true security. Encryption is.
I used the above email code in my office system but i get operation timed out exception.
I tried with port 25 and 465 also but no luck. For port 465, i got Failure sending mail exception.
The same code worked successfully in my system.

The work emails which i am using use 2 factor authentication.

Is there anyway i can resolve the issue?
 
Last edited:
WIth 2FA enabled you need to configure app password to send mail with that account: Manage app passwords for two-step verification
Thank you very much for the guidance. So, it means that in this line, I will use my email address and the app password that will be generated by the administrator, is this correct ?

C#:
 <network host="smtp.office365.com" port="587" defaultCredentials="false" enableSsl="true"
                 userName="email@email.com" password="ThisPwd" targetName="STARTTLS/smtp.office365.com"
 
Back
Top Bottom