Windows service implementation automatically sends mail notifications

Source: Internet
Author: User
Tags time interval mailmessage smtpclient

Brief introduction

Microsoft's Windows Services, formerly known as NT Services. It allows you to create a long-running executable application based on your own system session. These services can be started automatically when the boot, can be aborted, restarted, and not realistic any user interface. When you use a server or you need to run on a machine for a long time and you can't interfere with other users using the computer, the features of the service make it an ideal technology.
A Windows service can be created by Microsoft's VS or Embarcadero Delphi development tools. In order for a program to truly become a service for Windows systems, a program can be started, stopped, and aborted by the Service control Manager. The Service Control Manager is a component of a Windows operating system that starts and stops services.
Code

Open Visual Studio

Open File-> new-> Project

Select Windows Service

Right-Service1.cs Select Properties in the Drop-down menu

Click in the property form to add an installer:

  • Click in the property form to add an installer:

  • Right-ServiceInstaller1, select Properties

  • Change DisplayName, ServiceName for Seramailservice

  • Startup type is auto start

  • Right-ServiceProcessInstaller1, then select Properties

    change user to LocalSystem

    Right-Service1.cs design file, select View Code

    In the OnStart method, enter the following code:

    The code is as follows Copy Code

    public void Getmail (object sender, System.Timers.ElapsedEventArgs args)
    {
    NetworkCredential cred = new NetworkCredential ("email@lafarge.com", "Password");
    MailMessage msg = new MailMessage ();
    Msg. To.add ("email@apsissolutions.com");
    Msg. Subject = "Welcome Jubayer";

    Msg. BODY = "Your Have successfully entered to Sera's world!!!";
    Msg. from = new MailAddress ("email@apsissolutions.com"); Your Email Id
    SmtpClient client = new SmtpClient ("smtp.gmail.com", 587);
    SmtpClient client1 = new SmtpClient ("smtp.mail.yahoo.com", 465);
    Client. Credentials = cred;
    Client. Enablessl = true;
    Client. Send (msg);
    }

    To keep this method running, we need to add the time interval.
    Before the OnLoad method, insert the following code:

  • The code is as follows Copy Code

    System.Timers.Timer Createordertimer;

    In the OnStart method, enter the following code:

  • The code is as follows Copy Code

    Createordertimer = new System.Timers.Timer ();
    createordertimer.elapsed + = new System.Timers.ElapsedEventHandler (getmail);
    Createordertimer.interval = 180000;//set Three minutes intervals
    Createordertimer.enabled = true;
    Createordertimer.autoreset = true;
    Createordertimer.start ();

    All the code is as follows:

  • The code is as follows Copy Code

    System.Timers.Timer Createordertimer;

    Public Service1 ()
    {
    InitializeComponent ();
    }

    protected override void OnStart (string[) args)
    {
    Createordertimer = new System.Timers.Timer ();
    createordertimer.elapsed + = new System.Timers.ElapsedEventHandler (getmail);
    Createordertimer.interval = 500;
    Createordertimer.enabled = true;
    Createordertimer.autoreset = true;
    Createordertimer.start ();
    }

    public void Getmail (object sender, System.Timers.ElapsedEventArgs args)
    {
    NetworkCredential cred = new NetworkCredential ("email@lafarge.com", "Password");
    MailMessage msg = new MailMessage ();
    Msg. To.add ("email@apsissolutions.com");
    Msg. Subject = "Welcome Jubayer";

    Msg. BODY = "Your Have successfully entered to Sera's world!!!";
    Msg. from = new MailAddress ("email@apsissolutions.com"); Your Email Id
    SmtpClient client = new SmtpClient ("smtp.gmail.com", 587);
    SmtpClient client1 = new SmtpClient ("smtp.mail.yahoo.com", 465);
    Client. Credentials = cred;
    Client. Enablessl = true;
    Client. Send (msg);
    }

    Now compile the service with Ctrl+shift+b.
    In command line mode, enter the code to install the InstallUtil.exe.

  •   code as follows copy code

    C:/windows/microsoft.net/framework/v2.0.50727/installutil.exe

    The instructions for installing the service are:

  •   code as follows copy code

    C:/windows/microsoft.net/framework/v2.0.50727>installutil.exe
    "C:/Documents and
    Settings/administrator/my documents/visual Studio 2008/projects/seramailservice/
    seramailservice/bin/debug/ SeraMailService.exe

    Enter the services.msc command in the form you are running to view the service for the operating system. Right-click the name of the service you installed and start the service.

  • The code is as follows Copy Code

    c:/windows/microsoft.net/framework/v2.0.50727>installutil.exe/u
    "C:/Documents and Settings/administrator/my documents/visual Studio 2008/
    Projects/seramailservice/seramailservice/bin/debug/seramailservice.exe "

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.