Creating an exe without using a Form

I have been using ASP.NET for some time, but now I need to be able to create a quick program that sends out an email if a file exist. A batch file will be executing this file on a scheduled basis. What is the best way to write this. (By the way, I have the code to do this, I did it as an ASP.NET application, now I want to port it to an executable). I am using VS.NET 2003.

Thanks.


Answer this question

Creating an exe without using a Form

  • jocamp3

    Ken, I can see where the Windows Service would be useful. By the way great tutorial on creating a Windows Service. I followed the article and was able to get your Service to work. For this FTP program I only need it to FTP to a site once every two weeks. Once I get the file I need to run another program that will decrypt the file. Will a Windows Service suffice and is there a component that will tell the service to start every Wednesday at 4:00 pm  

    Thanks for all the help, this is great feedback!!

  • rfmfix

    When I add SendMail() into:
    static void Main(string[] args)
    {
      SendMail();
    }

    I get this error:

    An object reference is required for the nonstatic field, method, or property 'ConsoleApp.Class1.SendMail()'

  • Tamer2

    Cool. Where I can I find more information on writing a service
  • truepantera

    joee:

    It's true that a console app is simpler, but console apps aren't meant to run nonstop without interruption. Just wanted to bring that up -- that's what services are for.

    In any case, you must add a reference to your project for the System.Web.dll assembly in order to send email, and from there, add code that calls the Send method of the System.Web.Mail.SmtpMail class. I'm not sure where the SendMail method came from, but it's not provided by the .NET Framework.

  • MikeApplied

    Just so happens I wrote an article on this for MSDN magazine a few years back <g>. Check this <a href="http://msdn.microsoft.com/msdnmag/issues/01/12/NETServ/default.aspx">link</a>.
  • TimBemis

    If you're just going to run the application once evey two weeks, I'm not sure you need a service. I thought it was to be running continuously. Certainly, you could run the service and have it only "wake up" every two weeks, but that seems silly. Better to use the Windows scheduler just to run your console application on schedule, and have it wait for the file to be delivered. I don't know much about programming the windows scheduler, but it's possible. 
  • psilos2000

    Just a couple of quick questions...

    1.  Are you creating the console app from Visual Studio or another text editor   

    2.  Have you included a reference to the dll containing SendMail object


  • hopark16

    A Console application is the best way.
  • Scott Swigart - MVP

    If you want a quick program a console app will be easier to develop, using scheduler to run at set intervals.  If you want an application that runs independent of the logged on user then a windows service is the better choice.

    In answer to your question about a console app "Page_Load", a console app has a <b>Main</b> function which is called when the app is executed, for example...

    using System.Console;

    class MyHello
    {
         static void Main()
         {
             WriteLine("This is a console hello");
          }
    }

    In the <b>Main</b> you can initialise any object you wish to use.

    Hope this helps

  • Paul Reith

    So in my ConsoleApp I have a function called SendMail in my Class1.cs. How do I initialize that function. In ASP.NET you have a Page_Load function.
  • Oliver 123

    Incidentally, the reason for your error was that SendMail() needs to be static.  Main() runs before *everything*, so not even your class is instantiated.  You can, if you want, instantiate your class and call SendMail(), like this:

    static void Main(string[] args)
    {
       MyConsoleApp myapp = new MyConsoleApp();
       myapp.SendMail();
    }

    Or you can make SendMail() static and call it from Main() like you did:

    public static void SendMail()
    {
    // send out mail
    }

    static void Main(string[] args)
    {
       SendMail();
    }

    -Ari

  • ladymuck

    If you want to have some code execute at a regular interval, and you're running on Windows NT or later (that is, not Windows 98 or ME), you might consider creating a Windows Service. It's easy to do, and will allow you to run the code whether or not a user is logged in.
  • Creating an exe without using a Form