限制每天向用户发送一封电子邮件 [英] Restrict to send emails to a user at one per day

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

问题描述

我已经编码,只要用户点击按钮就会发送电子邮件。现在的任务是,我们需要将发送给用户的电子邮件限制为每天一封,尽管每天都会多次点击按钮。我到目前为止所做的代码是



I have till now coded to send email whenever the user clicks on the button. Now the task is that we need to restrict the emails sent to the user to one per day in spite of clicking the button several times a day. The code I have done till now is

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
            {
                UserDetailService userDetailsService = new UserDetailService();
                var boroughContacts = from p in SessionContents.CurrentSchool.Borough.BoroughContacts
                               select p;
                
                if (boroughContacts != null)
                {
                    foreach (BoroughContact thisBoroughContact in boroughContacts)
                    {
                        if (thisBoroughContact.Contact != null)
                        {
                            UserDetailCollection userWithReceiveMail = userDetailsService.ReadAllUserWithRecieveMail((int)SessionContents.CurrentBorough.BoroughID, (int)thisBoroughContact.Contact.ContactID);
                            if (userWithReceiveMail.Count > 0)
                            {
                                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)
            {
                //CDS.Framework.Library cdsLibrary = new CDS.Framework.Library(Common.ApplicationName);
                ApplicationSettings appSettings = new ApplicationSettings(Common.ApplicationName);

                //string userPassword = cdsLibrary.DecryptString(thisUserDetail.Password, true);
                //string emailSubject = "TfL Stars Account";

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





我尝试了什么:



任何人都可以帮我完成这项任务。



What I have tried:

Can anyone please help me to complete this task.

推荐答案

最简单的方法是存储一个标记该电子邮件已在该特定日期发送给该特定用户。



因此,在将用户添加为电子邮件更改的收据之前,如果该标志存在。如果它没有发送电子邮件然后设置标志。如果该标志确实存在停止处理并且不将该用户添加为收据。
The simplest way is to store a flag that the email has already been sent to that particular user on that particular day.

So before you add the user as a receipt of the email change if the flag exist. If it does not send the email and then set the flag. If the flag does exist stop processing and do not add the user as a receipt.


要扩展解决方案1,该标志应以非易失性方式存储。



这可以是SQL表格,也可以是平面文件。



取决于你的意思每天(日历日或24小时?),你可以删除过期条目。



现在 - 假设你使用SQL:你可以做一个简单的查询收件人(如计数),如果发现它们存在(计数> 0),则停止发送。如果它不存在,请将它们的记录添加到表中并处理发送。根据您计算一天的方式,您可以从表中删除记录(或更新它们或您喜欢的任何机制)。当然,使用时间戳,例如SQL的getdate()。



平面文件:您只需在文件中放入一个标识值即可。打开以进行读取/追加,如果找不到该名称,请将其附加。如上所述处理邮寄。如果您的日期是日历日,则可以在每天午夜删除该文件。
To expand upon solution 1, the flag should be stored in a non-volatile manner.

This could either be in a SQL table or even a flat-file.

Depending upon what you mean by once-per day (calendar day or 24hr period?), you can remove "expired" entries.

Now - suppose you use SQL: you can do a simple query for the recipient (such as count) and if they're found to exist (count>0) then you stop sending. If it doesn't exist, add the record for them to the table and process the send. Depending upon the way you count a day, you can remove records from the table (or update them or whatever mechanism you prefer). Use a timestamp, of course, such as SQL's getdate().

Flat file: You could simply put an identifying value in the file. Open for read/append and if you cannot find the name, append it. Handle mailing as above. If your day is a calendar day, you can delete the file at midnight each day.


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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆