Click here to Skip to main content
Click here to Skip to main content

Send Auto Notification Using Windows Service

, 1 Jan 2014
Rate this:
Please Sign up or sign in to vote.
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:
    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:

    System.Timers.Timer createOrderTimer;

    In the onStart method, type this 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();

The full code is as given below:

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)

About the Author

Ahmed Jubayer
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

 
QuestionThanks good article PinmemberMember 100598316-Jan-14 7:41 
AnswerRe: Thanks good article PinmemberAhmed Jubayer19-Jan-14 5:24 
GeneralMy vote of 1 Pinmemberbezveza3-Jan-14 2:27 
GeneralMy vote of 1 PinmemberAntonio Ripa31-Dec-13 2:21 
GeneralMy vote of 1 PinmemberVolkito30-Dec-13 21:29 
GeneralRe: My vote of 1 [modified] PinmemberAhmed Jubayer31-Dec-13 20:31 
QuestionNot ASP.NET PinmemberDharmesh Kemkar30-Dec-13 15:41 
AnswerRe: Not ASP.NET PinprofessionalWebBHTAN30-Dec-13 19:48 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 1 Jan 2014
Article Copyright 2013 by Ahmed Jubayer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid