0

I am working on Leave Management system, it is my company internal project. For this i placed fields like leave from date, to date, reporting manager submit button in leave management form, user enter these fields and click submit button mail(To: Reporting Manager, Cc: HR) goes to reporting manager from user(who is person leave applied) account, up to this working fine.

Now i got new requirement, User enter the leave management fields and submitted(user applying leave), this leave request goes to his reporting manager. Reporting manager not approved user leave request up to 3 days, that time the mail will triggered automatically for 3 every days once, up to 3 times from user mail account.

For example Today(21-08-2013) i applied leave, leave request goes to my reporting manager from my my mail account while applying time. my reporting manager not approved my leave request up to 3 days means 24-08-2013, this date mail should be send automatically from my account to reporting manager account, he not approved 24-08-2013 date also, again mail triggered automatically from my account to reporting manager account in 27-08-2013, if he not approved in 27-08-2013 also, mail triggered in 30-08-2013, even 28-08-2013 also not approved mail triggered automatically to my HR mail account(applying leave request time Cc mail goes HR).

Below the code in submit button in Leave management form:

 protected void BtnApplyLeave_Click(object sender, EventArgs e) { if (ddlleavetype.SelectedIndex == 0) { lblerrmsg.Text = "Please select Leavetype"; lblerrmsg.Visible = true; } else if (txtmbno.Text == "") { lblerrmsg.Text = "Please Enter MobileNO"; lblerrmsg.Visible = true; } else if (txtfromdate.Text == "") { lblerrmsg.Text = "please Select From date"; lblerrmsg.Visible = true; } else if (txttodate.Text == "") { lblerrmsg.Text = "please Select todate"; lblerrmsg.Visible = true; } else if (txtReasonleave.Text == "") { lblerrmsg.Text = "Please Enter Reason for Leave"; lblerrmsg.Visible = true; } else { string username = Session["logUser"].ToString(); if (ddlleavetype.SelectedValue == "Earned Leave") { try { OleDbCommand com = new OleDbCommand("update IND_EMP_LEAVES set leavefromdate = to_date('" + txtfromdate.Text + "','dd/MM/yyyy'), leavetodate = to_date('" + txttodate.Text + "','dd/MM/yyyy'), MBNO = '" + txtmbno.Text + "', NOOFDAYSAPPL = '" + txtleavedays.Text + "', LEAVETYPE = '" + ddlleavetype.SelectedValue + "', REASONFORLEAVE = '" + txtReasonleave.Text + "', STATUS = '" + "null" + "' where Username ='" + username + "'", DbConnection); DbConnection.Open(); com.ExecuteNonQuery(); DbConnection.Close(); } catch (Exception ex) { ex.ToString(); } } else if (ddlleavetype.SelectedValue == "Sick Leave") { try { OleDbCommand com = new OleDbCommand("update IND_EMP_LEAVES set leavefromdate = to_date('" + txtfromdate.Text + "','dd/MM/yyyy'), leavetodate = to_date('" + txttodate.Text + "','dd/MM/yyyy'), MBNO = '" + txtmbno.Text + "', NOOFDAYSAPPL = '" + txtleavedays.Text + "', LEAVETYPE = '" + ddlleavetype.SelectedValue + "', REASONFORLEAVE = '" + txtReasonleave.Text + "', STATUS = '" + "null" + "' where Username ='" + username + "'", DbConnection); DbConnection.Open(); com.ExecuteNonQuery(); DbConnection.Close(); } catch(Exception ex) { ex.ToString(); } } else if (ddlleavetype.SelectedValue == "LOP") { try { OleDbCommand com = new OleDbCommand("update IND_EMP_LEAVES set leavefromdate = to_date('" + txtfromdate.Text + "','dd/MM/yyyy'), leavetodate = to_date('" + txttodate.Text + "','dd/MM/yyyy'), MBNO = '" + txtmbno.Text + "', NOOFDAYSAPPL = '" + txtleavedays.Text + "', LEAVETYPE = '" + ddlleavetype.SelectedValue + "', REASONFORLEAVE = '" + txtReasonleave.Text + "', STATUS = '" + "null" + "' where Username ='" + username + "'", DbConnection); DbConnection.Open(); com.ExecuteNonQuery(); DbConnection.Close(); } catch (Exception ex) { ex.ToString(); } } try { string subject = "Reg: Leave Request"; string datefrom = txtfromdate.Text; string dateto = txttodate.Text; string msg = txtReasonleave.Text; DbConnection.Open(); OleDbCommand cmd = new OleDbCommand("select EMP_NAME, REPT_MGR, hr from EMP_HIERARCHY where username ='" + username + "'", DbConnection); OleDbDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { string ename = dr[0].ToString(); string rep_mgr = dr[1].ToString(); string hr_mgr = dr[2].ToString(); Session["emp"] = ename; Session["mgr"] = rep_mgr; Session["hr"] = hr_mgr; } dr.Close(); string emp = Session["emp"].ToString(); OleDbCommand cmd7 = new OleDbCommand("select M.OFFICIAL_EMAIL, L.pwd from EMP_MASTER M, emplogin L where L.username = '" + username + "' and M.username = L.username", DbConnection); OleDbDataReader dr1 = cmd7.ExecuteReader(); while (dr1.Read()) { string frommail = dr1[0].ToString(); string frompwd = dr1[1].ToString(); Session["from"] = frommail; Session["pwd"] = frompwd; } dr1.Close(); string mgr = Session["mgr"].ToString(); string hr = Session["hr"].ToString(); OleDbCommand cmd2 = new OleDbCommand("select OFFICIAL_EMAIL from emp_master where emp_name = '" + mgr + "'", DbConnection); OleDbDataReader dr2 = cmd2.ExecuteReader(); while (dr2.Read()) { string rep_to = dr2[0].ToString(); Session["to"] = rep_to.ToString(); } dr2.Close(); OleDbCommand cmd3 = new OleDbCommand("select OFFICIAL_EMAIL from emp_master where emp_name = '" + hr + "'", DbConnection); OleDbDataReader dr3 = cmd3.ExecuteReader(); while (dr3.Read()) { string hr_to = dr3[0].ToString(); Session["Cc"] = hr_to.ToString(); } dr3.Close(); string body = string.Concat("I would like to apply '" + ddlleavetype.SelectedValue + "' from ", datefrom, " to ", dateto, System.Environment.NewLine, "Reason: ", msg); string from = Session["from"].ToString(); string pwd = Session["pwd"].ToString(); string to = Session["to"].ToString(); string Cc = Session["Cc"].ToString(); var smtp = new System.Net.Mail.SmtpClient(); { smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.EnableSsl = true; smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtp.Credentials = new NetworkCredential(from, pwd); smtp.Timeout = 20000; } //smtp.Send(from, to, subject, body); // Instantiate a new instance of MailMessage MailMessage mMailMessage = new MailMessage(); // Set the sender address of the mail message mMailMessage.From = new MailAddress(from); // Set the recepient address of the mail message mMailMessage.To.Add(new MailAddress(to)); // Check if the bcc value is null or an empty string //if ((bcc != null) && (bcc != string.Empty)) //{ // // Set the Bcc address of the mail message // mMailMessage.Bcc.Add(new MailAddress(bcc)); //} // Check if the cc value is null or an empty value if ((Cc != null) && (Cc != string.Empty)) { // Set the CC address of the mail message mMailMessage.CC.Add(new MailAddress(Cc)); } // Set the subject of the mail message mMailMessage.Subject = subject; // Set the body of the mail message mMailMessage.Body = body; // Set the format of the mail message body as HTML mMailMessage.IsBodyHtml = true; // Set the priority of the mail message to normal mMailMessage.Priority = MailPriority.Normal; // Instantiate a new instance of SmtpClient //SmtpClient smtp = new SmtpClient(); // Send the mail message smtp.Send(mMailMessage); ScriptManager.RegisterStartupScript(this, GetType(), "success", "alert('Leave Request sent Successfully');", true); // Session.Clear(); } catch (Exception ex) { ex.ToString(); } } } 
1
  • Would be open to adding "Quartz" framework to your application.? Commented Aug 21, 2013 at 11:45

2 Answers 2

2

Quartz.NET allows periodic scheduling of jobs. You can use it to schedule emails. This blog gives a nice example of how to use Quartz in your app.

Here is another thread which you might find useful: How to setup Quartz.NET for scheduling Emails

Sign up to request clarification or add additional context in comments.

2 Comments

i don't about Quartz, can you give .net related answers
Quartz has a .NET api too and is widely used in industry to schedule jobs.
0

You need to create a service for to achieve this.

Please have a look at following article-
Create a service to send a mail automatically in asp.net using c#.net

In this article, a service is created which will be executed at settled time and a mail will be sent.

2 Comments

But my mail sending code is in my Leave management application, i sent mails based on my leave management fields. so how to include mail code in to windows service
A service is one way to do it. Certainly not the only way. As others have said, you can use Quartz.NET. You can also use the Windows API to create scheduled tasks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.