Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a button name "Unlock sign Off page". Each time when a user clicks on the button an email will be generated and sent to the contact. Now the user feel that it is annoying and they want to limit the number the email sent to one per day. I have the below set of code to generate the email and send to the contact.

C#
protected void SendUnlockRequest()
    {
        if (SessionContents.CurrentSchool != null && SessionContents.CurrentUser != null)
        {
            ContactCollection contactsToEmail = new ContactCollection();
            string emailMessage = "";
            string emailSubject = "";

            if (SessionContents.CurrentUser.UserType.Name == "YTA")
            {
                var schoolContacts = from p in SessionContents.CurrentSchool.SchoolContacts
                               where p.PrimaryContact == true
                               select p;

                if (schoolContacts != null)
                {
                    foreach (SchoolContact thisSchoolContact in schoolContacts)
                    {
                        if (thisSchoolContact.Contact != null)
                        {
                            contactsToEmail.Add(thisSchoolContact.Contact);
                        }
                    }
                }
                emailSubject = "TfL Stars Project unlock request";
                emailMessage = "A YTA user at your school, " + SessionContents.CurrentUser.Contact.ContactName + "("
                    + SessionContents.CurrentUser.Contact.EmailAddress + "), has requested that their project be unlocked.";
            }
            else
            {
                var boroughContacts = from p in SessionContents.CurrentSchool.Borough.BoroughContacts
                               select p;

                if (boroughContacts != null)
                {
                    foreach (BoroughContact thisBoroughContact in boroughContacts)
                    {
                        if (thisBoroughContact.Contact != null)
                        {
                            contactsToEmail.Add(thisBoroughContact.Contact);
                        }
                    }
                }
                emailSubject = "TfL Stars Travel Plan unlock request";
                emailMessage = "A school user at a school within your Borough, " + SessionContents.CurrentSchool.Name + " - " 
                    + SessionContents.CurrentUser.Contact.ContactName + "("
                    + SessionContents.CurrentUser.Contact.EmailAddress + "), has requested that their travel plan be unlocked.";
            }
            foreach (Contact thisContact in contactsToEmail)
            {
                 ApplicationSettings appSettings = new ApplicationSettings(Common.ApplicationName);

                StringBuilder sbEmailBody = new StringBuilder();
                sbEmailBody.Append("Dear Stars User\n\n");
                sbEmailBody.Append(emailMessage);
                sbEmailBody.Append("\n\n");
                sbEmailBody.Append("If you have any problems please contact the Stars support team.\n\n");

                EmailUtil.SendEmail(appSettings.SupportFromEmailAddress, "TfL Stars", thisContact.EmailAddress,
                    thisContact.ContactName, emailSubject, sbEmailBody.ToString());
            }
        }
    }


can anyone help me to restrict the number of emails sent to one per day.
Posted

1 solution

Create a table with three columns; contact id, date and count. This will store how many times an email has been sent to each contact and the date the mail was sent, so;

SQL
id, date, count
---------------
123, 1 Sep 2015, 3


means contact 123 has been sent 3 emails on 1st Sept. Before you send an email get the date and count for the current contact.

If no row exists for the current contact then create one with today's date and 1 as the count and send the email.

If the date is not today then update the record for that contact with today's date and 1 for the count and send the email

If the date is today then update the row to increate the count by 1, and if the count exceeds the limit then don't send the email, otherwise send it.
 
Share this answer
 
v3
Comments
Schatak 2-Sep-15 0:37am    
Nice :)

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