如何每3天自动触发邮件,最多触发3次

本文关键字:3次 何每 3天 | 更新日期: 2023-09-27 18:28:24

我正在开发休假管理系统,这是我公司的内部项目。为此,我在休假管理表单中放置了休假开始日期、截止日期、报告经理提交按钮等字段,用户输入这些字段并单击提交按钮,邮件(收件人:报告经理,抄送:人力资源)从用户(申请休假的人)帐户转到报告经理,直到该工作罚款。

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

例如,今天(2013年8月21日)我申请了休假,在申请时间时,休假请求从我的邮件账户发送给了我的报告经理。我的报告经理在3天内没有批准我的休假申请,这意味着2013年8月24日,这个日期的邮件应该从我的账户自动发送到报告经理账户,他也没有批准2013年08月24日的日期,再次在2013年08年08月27日从我的帐户自动触发到报告经理帐户的邮件,如果他在2013年07月27日也没有批准,则在2013年09月30日触发邮件,即使在2013年8月28日,我的人力资源邮件账户也没有自动触发批准邮件(申请休假时间抄送给人力资源)。

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

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

如何每3天自动触发邮件,最多触发3次

Quartz.NET允许定期安排作业。你可以用它来安排电子邮件。这个博客提供了一个如何在应用程序中使用Quartz的好例子。

这里有另一个你可能会觉得有用的线程:如何设置Quartz.NET来调度电子邮件

您需要为创建一个service来实现这一点。

请看下面的文章-
创建一个服务,使用c#.net 在asp.net中自动发送邮件

在本文中,创建了一个服务,该服务将在结算时间执行,并发送邮件。