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 " |