Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
I have one requirement like to send 25 mail every week.So what i did,I took one count and made count++ on sending every mail.When it reached to 25 it displays alert already crossed limit for this week.But my problem is that how to reset that count on start of the week i.e on Monday.
Here some code that i have written.
C#
try
        {
            string body = "";
            string tbl_custInfo = "";
            int countno1=1;
            int countno=1;
            string xmlfile = Server.MapPath("App_code/count.xml").Trim().ToLower();
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(xmlfile);

            XmlNodeList CountNodeList = xmldoc.SelectNodes("//section");

            foreach (XmlNode CountNode in CountNodeList)
            {
                if (CountNode.HasChildNodes)
                {
                    countno =Convert.ToInt32(CountNode.ChildNodes[0].InnerText.Trim().ToLower());
             
             var testDay = DateTime.Now.DayOfWeek.ToString();
             //if (testDay == "Sunday")
             //if (testDay == "Monday" && countno != 1)
             //{

             //    CountNode.ChildNodes[0].InnerText = countno1.ToString();
              

             //}
            
             if ((testDay == "Monday" || testDay == "Tuesday" || testDay == "Wednesday" || testDay == "Thursday" || testDay == "Friday" || testDay == "Saturday" || testDay == "Sunday") && (countno <= 3))
            {
               
            
            tbl_custInfo = "<table style='border:solid 1px #eee;padding-removed30px;' align='center' width='100%'>";
            tbl_custInfo += "<tr><td colspan='2' style='border-removedsolid 1px #aec6d8;text-align:center;font-weight:bold;color:#8d8f8a;'>Customer Contact Details</td></tr>";
            tbl_custInfo += "<tr><td style='border-removedsolid 1px #eee;'>Name :</td><td style='border-removedsolid 1px #eee;'>" + txtName.Value.Trim() + "</td></tr>";
            tbl_custInfo += "<tr><td style='border-removedsolid 1px #eee;'>Phone :</td><td style='border-removedsolid 1px #eee;'>" + txtPhone.Value.Trim() + "</td></tr>";
           
            tbl_custInfo += "<tr><td style='border-removedsolid 1px #eee;'>Email :</td><td style='border-removedsolid 1px #eee;'>" + txtEmail.Value.Trim() + "</td></tr>";

            tbl_custInfo += "<tr><td style='border-removedsolid 1px #eee;'>Message :</td><td style='border-removedsolid 1px #eee;'>" + txtComment.Value.Trim() + "</td></tr>";
            tbl_custInfo += "</table><br/><br/>";

            body = "<br/>" + tbl_custInfo;
           
            SmtpClient sc = new SmtpClient();
            MailMessage mm = new MailMessage();

            if (ConfigurationManager.AppSettings["MailFrom"].ToString().Trim() != "")
            {
                mm.From = new MailAddress(ConfigurationManager.AppSettings["MailFrom"].ToString().Trim(), txtEmail.Value.ToString().Trim());
            }
            if (ConfigurationManager.AppSettings["MailTo"].ToString().Trim() != "")
            {
                mm.To.Add(ConfigurationManager.AppSettings["MailTo"].ToString().Trim());
            }
            if (ConfigurationManager.AppSettings["MailBCc"].ToString().Trim() != "")
            {
                mm.Bcc.Add(ConfigurationManager.AppSettings["MailBCc"].ToString().Trim());
            }
            mm.Subject = ConfigurationManager.AppSettings["Subject"].ToString().Trim();
            mm.ReplyTo = new MailAddress(txtEmail.Value);
            mm.Body = body;

            mm.IsBodyHtml = true;

            sc.Send(mm);
            
            
            countno1 = countno1 + 1;
            CountNode.ChildNodes[0].InnerText = countno1.ToString();
          
            xmldoc.Save(xmlfile);
            this.ClientScript.RegisterStartupScript(this.GetType(), "animo", "<script language=\"javaScript\">" + "alert('Thanks for Contacting Us.');" + "window.location.href='contact.aspx';" + "<" + "/script>");

     }
    else
    {
                this.ClientScript.RegisterStartupScript(this.GetType(), "animo", "<script language=\"javaScript\">" + "alert('no more available time slots and to try again in 14 (or 7) days.');" + "window.location.href='contact.aspx';" + "<" + "/script>");
            }
          
       }
          
                  }
        }
        catch { }

Please help me out.
Posted

1.You should implement this into a windows service application and not into a web (ASP.NET) application, because the web application is activated only if at least one user is accessing it.

2.So the solution is, to implement a windows service application and inside of it you could use Timer like was suggested in the 1st solution above, but better is to use Thread and inside of the main thread to use Thread.Sleep (to put the thread to sleep for 24 hours) and then to check for Monday like in the example bellow:
C#
TimeSpan pauseTimeSpan = new TimeSpan(24, 0, 0); //24 hours!
TimeSpan weekTimeSpan = new TimeSpan(24*7, 0, 0); //7 days!
//
while (true)
                {
                    if(DateTime.Now.DayOfWeek == DayOfWeek.Monday) &&
                        (workerThread == null || !workerThread.IsAlive))
                    {
                        //
                        // Start the Worker thread for sending email notifications!
                        //
                        workerThread = new Thread(new ThreadStart(SendEmailNotifications));
                        workerThread.Start();
                        //
                        // Put the main thread in sleep for 7 days!
                        //
                        Thread.Sleep(weekTimeSpan);
                    }
                    else
                    {
                       //
                       // Pause the main thread only one day because could be the situation that
                       // the server will be restarted during the week!
                       //
                       Thread.Sleep(pauseTimeSpan);
                    }   
                    
                }
 
Share this answer
 
Comments
SanSkun 8-Aug-14 3:12am    
Hi RaulIloc.
The code is look like good for this.But how to restrict user to send email maximum up to 25 email for every week.
Raul Iloc 8-Aug-14 3:20am    
The method "SendEmailNotifications" function from my solution above, should in fact reset the counter to 0 then save its value into the database (or into an XML file). Then before to send notifications you should read the counter value from its cache (database or XML file) and compare it with 24. If is lower then 24 you could increment it, send the email then save it again into the cache (database or XML file).
You can use a System.Timer[^] object.
 
Share this answer
 
v2
Comments
SanSkun 8-Aug-14 1:20am    
Can u please provide here that logic with sample code..Please
Please go though the codes here in this Link[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900