限制从服务器发送的电子邮件每天一个

限制从服务器发送的电子邮件每天一个

本文介绍了限制从服务器发送的电子邮件每天一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮名称Unlock sign Off page。每次用户点击按钮时,都会生成一封电子邮件并发送给该联系人。现在用户觉得它很烦人,他们想要限制每天发送一封电子邮件的数量。我有以下一组代码来生成电子邮件并发送给联系人。



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.

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.

推荐答案

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





表示联系人123已于9月1日发送了3封电子邮件。在您发送电子邮件之前,请获取当前联系人的日期和计数。



如果没有行存在当前联系人,然后创建一个今天的日期和1作为计数并发送电子邮件。



如果日期不是今天,那么更新该联系人的记录今天的日期和1的计数和发送电子邮件



如果日期是今天然后更新行,将计数增加1,如果计数超过限制然后不发送电子邮件,否则发送。



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.


这篇关于限制从服务器发送的电子邮件每天一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 01:52