public IHttpActionResult LeaveApplication(appliedleave objLeaveMatrix) { try { Leave_Approval_Matrix objLeave_Approval_Matrix = new Leave_Approval_Matrix(); objLeave_Approval_Matrix.userId = objLeaveMatrix.userId; objLeave_Approval_Matrix.startDate = Convert.ToDateTime(objLeaveMatrix.startDate); objLeave_Approval_Matrix.endDate = Convert.ToDateTime(objLeaveMatrix.endDate); objLeave_Approval_Matrix.daysCount = (objLeave_Approval_Matrix.endDate - objLeave_Approval_Matrix.startDate).Days + 1; objLeave_Approval_Matrix.reason = objLeaveMatrix.reason; objLeave_Approval_Matrix.status = 1; objLeave_Approval_Matrix.approverId = objLeaveMatrix.approverId; objLeave_Approval_Matrix.LevelId = 1; objLeave_Approval_Matrix.LeaveTypeId = objLeaveMatrix.LeaveTypeId; objLeave_Approval_Matrix.createdDate = DateTime.Now; objLeave_Approval_Matrix.updateDate = DateTime.Now; db.Leave_Approval_Matrix.Add(objLeave_Approval_Matrix); db.SaveChanges(); //////////////////////Sending Mail//////////////////////////// var approver_email = db.Users.Find(objLeaveMatrix.approverId).email; using (MailMessage mail = new MailMessage()) { mail.From = new MailAddress(System.Configuration.ConfigurationManager.AppSettings["emailFrom"]); mail.To.Add(approver_email); mail.Subject = System.Configuration.ConfigurationManager.AppSettings["leaveRequestsubject"] + " " + "from" + " " + db.Users.Where(x => x.Id == objLeaveMatrix.userId).FirstOrDefault().firstName + " " + db.Users.Where(x => x.Id == objLeaveMatrix.userId).FirstOrDefault().lastName; mail.Body = replaceLeaveApplicationContentHTML(objLeaveMatrix); mail.IsBodyHtml = true; using (SmtpClient smtp = new SmtpClient(System.Configuration.ConfigurationManager.AppSettings["smtpAddress"], Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["portNumber"]))) { smtp.Credentials = new NetworkCredential(mail.From.ToString(), System.Configuration.ConfigurationManager.AppSettings["password"]); smtp.EnableSsl = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["enableSSL"]); smtp.Send(mail); } return(Ok("Saved")); } } catch (Exception e) { return(Ok(e.InnerException)); } }
public string replaceLeaveApplicationContentHTML(appliedleave objLeaveMatrix) { string body = string.Empty; //using streamreader for reading my htmltemplate using (StreamReader reader = new StreamReader(System.Web.Hosting.HostingEnvironment.MapPath("~/HtmlTemplate/Email_HTML.html"))) { body = reader.ReadToEnd(); } body = body.Replace("{ApporverFirst name}", db.Users.FirstOrDefault(x => x.Id == objLeaveMatrix.approverId).firstName); //replacing the required things body = body.Replace("{ApporverLast Name}", db.Users.FirstOrDefault(x => x.Id == objLeaveMatrix.approverId).lastName); body = body.Replace("{requesterFirst Name}", db.Users.FirstOrDefault(x => x.Id == objLeaveMatrix.userId).firstName); body = body.Replace("{requesterLast Name}", db.Users.FirstOrDefault(x => x.Id == objLeaveMatrix.userId).lastName); body = body.Replace("{reason}", objLeaveMatrix.reason); return(body); }