News
IBM to offer mobile security as a service IBM will start delivering mobile security-as-a-service (MaaS) through its own cloud infrastructure in India, a move that is expected to better address in-country data requirements... IBM Spectrum Computing: IBM has enlarged its portfolio of software-defined infrastructure solutions with cognitive features for workload management.  * LiFi internet technology has been introduced, the new type of wireless internet connection that gives 100 times faster than traditional WiFi -- Invented by Professor Harald Haas from the University of Edinburgh. * Sci. Rachid Yazami has developed a smart chip that charges smartphones in less than 10 minutes. BenQ has launched BlueCore projector - Consumer electronics major BenQ has launched its first BlueCore laser light source projector. For those unaware, devices with BlueCore laser technology have a high contrast output of 80000:1 with an extended lamp life and efficiency. *** 
  May 30 2015 1:04PM     John
  0 Comments    3289 Views  
In this article John demonstrates, how to Send an Employee Alert Mail for Every Morning Using Windows Service application in C#.
Steps to create Windows service
1. To Create a windows service, open Visual Studio, on the menu bar, choose File, New, Project.

2. In the list of Visual Basic or Visual C# project templates, choose Windows Service, and name the project MyNewService.

3. Choose OK.

4. The project template automatically adds a component class named Service1 that inherits from System.ServiceProcess.ServiceBase.

5.And then right click on Service1.Disigner.cs, click on View Code and paste the following code.
Right click Service1.Disigner.cs and click on View Code .
Service1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        Thread mainThreadObj;

        DateTime currentDateTime;

        Thread objProcessThread;
        
        string recievermailid = "xxxxxxx";
        string subject        = "Employee Alert Mail";
        string bodyText       = "This is an Employee Alert Mail.";

        public Service1()
        {
            InitializeComponent();
        }

        public void start()
        {

            string[] args = default(string[]);
            OnStart(args);
        }

        protected override void OnStart(string[] args)
        {
            currentDateTime = DateTime.Now;

            mainThreadObj = new Thread(ProcessThread);

            mainThreadObj.Start();

        }

        protected override void OnStop()
        {
        }

        private void ProcessThread()
        {
            if (objProcessThread == null ||
            objProcessThread.ThreadState == System.Threading.ThreadState.Stopped ||
            objProcessThread.ThreadState == System.Threading.ThreadState.Unstarted)
            {


                while (DateTime.Now >= currentDateTime.AddHours(12))//Every 12 Hours
                {
                    objProcessThread = new Thread(threadProcessMethod);

                    objProcessThread.IsBackground = true;

                    objProcessThread.Start();

                    currentDateTime = currentDateTime.AddSeconds(10);

                    Thread.Sleep( 12 * (60 * (1000 * 60))); //Every 12 Hours

                    ProcessThread();
                }

                Thread.Sleep( 12 * (60 * (1000 * 60))); //Every 12 Hours

                ProcessThread();
            }
        }

        private void threadProcessMethod()
        {
            sendmail(recievermailid,subject,bodyText)
        }
        public bool sendmail(string recievermailid, string subject, string bodyText)
        {
        try
        {
            string senderId = "********"// Sender EmailID
            string senderPassword = "*********"// Sender Password

            System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
            mailMessage.To.Add(recievermailid);
            mailMessage.From = new MailAddress(senderId);

            mailMessage.Subject = subject;
            mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;

            mailMessage.Body = bodyText;
            mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
            mailMessage.IsBodyHtml = false;

            mailMessage.Priority = MailPriority.High;

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Credentials = new System.Net.NetworkCredential(senderId, senderPassword);
            smtpClient.Port = 587;
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.EnableSsl = true;

            object userState = mailMessage;

            try
            {
                smtpClient.Send(mailMessage);
                return true;
            }
            catch (System.Net.Mail.SmtpException)
            {
                return false;
            }
        }
        catch (Exception)
        {
            return false;
        }
    }
}
Output
Build the windows services, you can find WindowsService1.exe in output console , some thing like this
D:\MySampleProjects\Service\MySampleWindows_Service\WindowsService1\bin\Debug\WindowsService1.exe
In the Command Prompt window, navigate to the folder that contains your project's output.
For example, under your
D:\MySampleProjects\Service\MySampleWindows_Service\WindowsService1\bin\Debug.
Enter the following command:
installutil.exe WindowsService1.exe
Here you can find more steps to install and uninstall the WindowsService
I hope this page will helps you to send Employee Alert Mail Using window service in C#.Thanks.
BackToTop
Comments



 
Search
Recent Posts
Create Amazon ElasticCache Using Memcached in CSharp
Nov 21 2017 12:09PM Posted By Amose
Get Google Map Lat Lng (Geo Point) By Pincode in C#
Nov 8 2017 10:26AM Posted By Pranav
Google URL Shortener in C#
Oct 26 2017 10:43AM Posted By Sanjay
Bind Gridview from CSV file in Asp.Net C#
Jun 18 2017 11:38PM Posted By Michael
Call WebService method from jQuery in every 1 minute
Feb 27 2017 11:51AM Posted By John
Gridview custom CSS in ASP.Net
Feb 25 2017 10:59AM Posted By Micheal Ryan
Read excel file and bind to Gridview in C#
Feb 25 2017 10:20AM Posted By Micheal
Tags
Follow us on Facebook
Follow us on Google +
Recent post in your Email inbox.
Enter your email address: