title: How to send automatic email using windows service in asp.net mvc-5 c# app
body: <p>I am working on an asp.net MVC-5 web project where i have to send daily email to all users. And i know there are 2 ways to achieve this:</p>
  
  <ol>
  <li>Windows service </li>
  <li>Sql server agent job.</li>
  </ol>
  
  <p>I have decided to use <em>Windows service</em>. I am working on Express Version of Visual Studio 2013 for Web, so Windows service template is not present in it. After digging a lot i have created a windows service as console application in Visual Studio. Here's the code:</p>
  
  <p><strong>Service</strong> </p>
  
  <pre><code>public class FirstService : ServiceBase
  {
          public FirstService()
          {
              InitializeComponent();
          }
  
          protected override void OnStart(string[] args)
          {
              this.EventLog.WriteEntry("FirstService Service Has Started");
          }
  
          protected override void OnStop()
          {
              this.EventLog.WriteEntry("FirstService Service Has Stopped");
          }
  
          private void InitializeComponent()
          {
              this.ServiceName = "FirstService";
              this.CanStop = true;
              this.AutoLog = false;
              this.EventLog.Log = "Application";
              this.EventLog.Source = "FirstService";
          }
  }
  </code></pre>
  
  <p><strong>Installer</strong></p>
  
  <pre><code>[RunInstaller(true)]
  public class MyServiceInstaller : Installer
  {
      private ServiceProcessInstaller FirstServiceProcessInstaller;
  
      private ServiceInstaller FirstServiceInstaller;
  
      public MyServiceInstaller()
      {
          InitializeComponent();
      }
  
      private void InitializeComponent()
      {
          this.FirstServiceProcessInstaller = new ServiceProcessInstaller();
          this.FirstServiceProcessInstaller.Account = ServiceAccount.LocalSystem;
          this.FirstServiceProcessInstaller.Username = null;
          this.FirstServiceProcessInstaller.Password = null;
          this.FirstServiceInstaller = new ServiceInstaller();
          this.FirstServiceInstaller.Description = "FirstService Service Template";
          this.FirstServiceInstaller.DisplayName = "First Service";
          this.FirstServiceInstaller.ServiceName = "FirstService";
          this.FirstServiceInstaller.StartType = ServiceStartMode.Manual;
          this.Installers.AddRange(new Installer[] { this.FirstServiceProcessInstaller, this.FirstServiceInstaller });
      }
  }
  </code></pre>
  
  <p><strong>Main</strong></p>
  
  <pre><code>static class Program
  {
      [STAThread]
      static void Main()
      {
          ServiceBase.Run(new FirstService());
      }
  }
  </code></pre>
  
  <p>After this i have successfully installed this service using <em>installutil.exe</em> and can successfully start and stop service from <em>Control Panel -> Administrative Tools -> Serivces</em>.
  Upto this everything is good. </p>
  
  <p>Now as i mention earlier i want to use windows service in my mvc-5 application for sending automatic email to my application users, i have some questions:</p>
  
  <ol>
  <li>How i will integrate Windows Service in my asp.net mvc-5 application?</li>
  <li>How i will deploy the application to the server?</li>
  <li>How i will install the windows service on my application hosting server?</li>
  </ol>
  
  <p>Please suggest me any good tutorial which contains the example, if possible.
  Thanks!</p>