title: send emails with templates in c# windows service
body: <p>I am new in windows services.</p>
  
  <p>I am facing some problems :-</p>
  
  <ol>
  <li>I have to reboot system every time to reinstall it.</li>
  <li>Can I debug my code in windows services?</li>
  <li>Is there any way to install it using command prompt (I have used
  Developer Command Prompt for VS2015).</li>
  </ol>
  
  <p>I have created a service to send simple emails and now I have changed the code to send emails with templates but its not working.</p>
  
  <p><strong>Error</strong> :- "Service Error on: 10/04/2017 07:40:00 PM Could not find a part of the path 'C:\Windows\system32\~\Payment-Receipt.htm'". </p>
  
  <p>File.ReadAllText is not replacement of Server.MapPath so what should be used?</p>
  
  <pre><code>private void SchedularCallback(object e)
  {
      try
      {               
          DataTable dt = new DataTable();                
          string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
          using (SqlConnection con = new SqlConnection(constr))
          {
              using (SqlCommand cmd = new SqlCommand("getAllInvoices"))
              {
                  cmd.Connection = con;
                  cmd.CommandType = CommandType.StoredProcedure;                       
                  using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                  {
                      sda.Fill(dt);
                  }
              }
          }                
          foreach (DataRow row in dt.Rows)
          {
              string name = row["FirstName"].ToString();
              string email = row["EmailId"].ToString(); 
              long InvoiceId = Convert.ToInt64(row["InvoiceId"].ToString());
              string amount = row["SettledAmount"].ToString();  
              WriteToFile("Trying to send email to: " + name + " " + email);                 
              string body = this.createEmailBody(name, "Please check your account Information", amount);
              using (MailMessage mm = new MailMessage(ConfigurationManager.AppSettings["UserName"], email))
              {
                  mm.Subject = "Package Changed";                        
                  mm.Body = body;
                  mm.IsBodyHtml = true;
                  SmtpClient smtp = new SmtpClient();
                  smtp.Host = ConfigurationManager.AppSettings["Host"];
                  smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
                  System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
                  credentials.UserName = ConfigurationManager.AppSettings["UserName"];
                  credentials.Password = ConfigurationManager.AppSettings["Password"];
                  smtp.UseDefaultCredentials = true;
                  smtp.Credentials = credentials;
                  smtp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
                  smtp.Send(mm);                                      
              }
          }
  
          this.ScheduleService();
      }
      catch (Exception ex)
      {
          WriteToFile("Service Error on: {0} " + ex.Message + ex.StackTrace);
        using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("SimpleService"))
          {
              serviceController.Stop();
          }
      }
  }
  
  private string createEmailBody(string userName, string title, string amount)
  {
      string body = string.Empty;        
      string originalTemplate = File.ReadAllText(@"~\Payment-Receipt.htm");
      using (StreamReader reader = new StreamReader(originalTemplate))
      {
          body = reader.ReadToEnd();
      }        
      body = body.Replace("{FirstName}", userName); //replacing the required things             
      body = body.Replace("{Amount}", amount);
      return body;
  }
  
  private void WriteToFile(string text)
  {
      string path = "C:\\ServiceLog.txt";
      using (StreamWriter writer = new StreamWriter(path, true))
      {
          writer.WriteLine(string.Format(text, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt")));
          writer.Close();
      }
  }
  </code></pre>