0

我正在开发休假管理系统,这是我公司的内部项目。为此,我在休假管理表单中放置了休假日期,至今,报告经理提交按钮等字段,用户输入这些字段并单击提交按钮邮件(收件人:报告经理,抄送:HR)从用户(谁是人假申请)帐户,直到这个工作正常。

现在我有了新的要求,用户输入休假管理字段并提交(用户申请休假),这个休假请求转到他的报告经理。报告经理最多 3 天未批准用户请假请求,届时邮件将每天自动触发 3 次,最多从用户邮件帐户触发 3 次。

例如今天(21-08-2013)我申请了休假,休假申请在申请时间时从我的邮件帐户发送给我的报告经理。我的报告经理在 3 天内未批准我的请假申请意味着 2013 年 8 月 24 日,该日期邮件应自动从我的帐户发送到报告经理帐户,他也未批准 2013 年 8 月 24 日的日期,再次自动触发邮件我的帐户在 27-08-2013 到报告经理帐户,如果他在 27-08-2013 也没有批准,邮件在 30-08-2013 触发,甚至 28-08-2013 也没有批准邮件自动触发到我的 HR 邮件帐户(申请休假请求时间抄送邮件去 HR)。

请假管理表单中提交按钮的代码下方:

    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();
            }
        }
    }
4

2 回答 2

2

Quartz.NET允许定期调度作业。您可以使用它来安排电子邮件。这个博客给出了一个很好的例子来说明如何在你的应用程序中使用 Quartz。

这是另一个您可能会发现有用的线程:如何设置 Quartz.NET 以安排电子邮件

于 2013-08-21T11:49:09.797 回答
0

您需要创建一个servicefor 来实现此目的。

请查看以下文章
-使用 c#.net 创建服务以在 asp.net 中自动发送邮件

在本文中,将创建一个服务,该服务将在确定的时间执行并发送一封邮件。

于 2013-08-21T11:46:14.917 回答