Click here to Skip to main content
15,881,424 members
Articles / Web Development / ASP.NET
Tip/Trick

Send Auto Notification Using Windows Service

Rate me:
Please Sign up or sign in to vote.
4.10/5 (18 votes)
1 Jan 2014CPOL1 min read 78.9K   2.3K   23   13
This tip describes how to send auto email using Windows service

Introduction

Microsoft Windows services, formerly known as NT services, enable you to create long-running executable applications that run in their own Windows sessions. These services can be automatically started when the computer boots, can be paused and restarted and do not show any user interface. These features make services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer.

A Windows service is created using development tools such as Microsoft Visual Studio or Embarcadero Delphi. In order to be a Windows service, a program needs to be written in such a way that it can handle start, stop, and pause messages from the Service Control Manager. Service Control Manager is a component of Windows which is responsible for starting and stopping services.

Using the Code

  • Open Visual Studio
  • Go to file->new->project
  • Select Windows service
  • Right click on Service1.cs and select properties
  • In the properties window, click on add Installer:

    Windows Service

  • Right click on serviceInstaller1, select properties
  • Change DisplayName, ServiceName to SeraMailService
  • StartType to Automatic

    Windows Service

  • Right click on serviceProcessInstaller1 then select Properties
  • Change Account to LocalSystem
  • Right click on Service1.cs Design select ViewCode

    Windows Service

  • In the OnStart method, type the following code:
    C#
    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 = "You 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 the method running, we add time interval.

    Add this line before onload method:

    C#
    System.Timers.Timer createOrderTimer;

    In the onStart method, type this code:

    C#
    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();

The full code is as given below:

C#
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 = "You 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 build the service using Ctrl+Shift+B.

In the command mode, type the code to install InstallUtil.exe.

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe

To install the service, use the command:

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"

Type services.msc in the run window and enter to see all services. Right click on the service, then run the service.

To uninstall the service, use this 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"

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer CodeMagnets Ltd.
Bangladesh Bangladesh
Hi, i'm Jubayer Ahmed.I started my programming career with c and c++.Later got a chance to develop windows/web applications using Java and ASP.Net.I Also developed some mobile application for android and iPhone. My interests involves programming, reading articles, know new technologies etc.

Comments and Discussions

 
QuestionApplication Pin
Member 150113673-Dec-20 12:56
Member 150113673-Dec-20 12:56 
Questionsome error Pin
Member 1046140430-Mar-15 20:36
Member 1046140430-Mar-15 20:36 
AnswerRe: some error Pin
Ahmed Jubayer16-Apr-15 5:48
professionalAhmed Jubayer16-Apr-15 5:48 
QuestionError Pin
Member 1134278827-Mar-15 0:23
Member 1134278827-Mar-15 0:23 
AnswerRe: Error Pin
Ahmed Jubayer29-Mar-15 19:07
professionalAhmed Jubayer29-Mar-15 19:07 
QuestionThanks good article Pin
Ron AF Greve6-Jan-14 7:41
Ron AF Greve6-Jan-14 7:41 
AnswerRe: Thanks good article Pin
Ahmed Jubayer19-Jan-14 5:24
professionalAhmed Jubayer19-Jan-14 5:24 
GeneralMy vote of 1 Pin
bezveza3-Jan-14 2:27
professionalbezveza3-Jan-14 2:27 
GeneralMy vote of 1 Pin
Antonio Ripa31-Dec-13 2:21
professionalAntonio Ripa31-Dec-13 2:21 
GeneralMy vote of 1 Pin
User 955792430-Dec-13 21:29
User 955792430-Dec-13 21:29 
GeneralRe: My vote of 1 Pin
Ahmed Jubayer31-Dec-13 20:31
professionalAhmed Jubayer31-Dec-13 20:31 
QuestionNot ASP.NET Pin
Dharmesh_Kemkar30-Dec-13 15:41
Dharmesh_Kemkar30-Dec-13 15:41 
AnswerRe: Not ASP.NET Pin
BH TAN30-Dec-13 19:48
professionalBH TAN30-Dec-13 19:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.