使用 c# 在 asp.net 中安排电子邮件 - 哪个事件或代码隐藏在哪个位置

发布于 2024-12-09 08:36:07 字数 166 浏览 0 评论 0原文

我想每天在特定时间向我的所有用户发送预定的电子邮件!
我应该使用哪个事件或哪个位置在后面的代码中发送它们?
我的页面基于母版页和内容页!
我应该使用母版页或内容页的 page_Load (Default.aspx)/还是应该使用 global.asax 来实现此目的?

提前致谢

i want to send scheduled emails to all of my users at specific time every day!
which event or which place should i use for sending them in code behind?
my pages are base on master and content pages!
should i use page_Load of master page or content page (Default.aspx) / or should i use global.asax for this purpose?

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花浅忆 2024-12-16 08:36:07

您应该使用 global.asax,在启动时启动一个线程并使用时间发送电子邮件。当应用程序关闭时停止线程。

您必须保持应用程序处于活动状态才能保持线程处于活动状态。如果您长时间没有任何请求,您的应用程序将被 iis 服务器关闭,您将不会发送电子邮件。

像这样的东西:

    private static bool stopProcessing;
    private static object __batchProcessLock;
    private static const int sleeptime = 2500;//2.5 seconds

    protected void Application_Start(object sender, EventArgs e)
    {
        Thread BatchThread = new Thread(Start);
    }

    protected void Application_End(object sender, EventArgs e)
    {
        Stop();
    }


    private static void Start()
    {
        lock (__batchProcessLock) // Be sure to run this only once in the application
        { 
            stopProcessing = false;
            while (!stopProcessing)
            {
                try
                {
                    //Do you stuff (check if it is time to send emails)
                }
                catch (Exception ex)
                {
                    //handle exception
                }
                Sleep(sleeptime); 
            }
        }
    }


    private static void Stop()
    {
        stopProcessing = true;
    }

You should use the global.asax, start a thread on startup and use a time to send emails. Stop the thread when the application closes.

You have to keep the application alive to keep the thread alive. If you do not have any request of a long time your application is closed by the iis server and you will not send emails.

something like:

    private static bool stopProcessing;
    private static object __batchProcessLock;
    private static const int sleeptime = 2500;//2.5 seconds

    protected void Application_Start(object sender, EventArgs e)
    {
        Thread BatchThread = new Thread(Start);
    }

    protected void Application_End(object sender, EventArgs e)
    {
        Stop();
    }


    private static void Start()
    {
        lock (__batchProcessLock) // Be sure to run this only once in the application
        { 
            stopProcessing = false;
            while (!stopProcessing)
            {
                try
                {
                    //Do you stuff (check if it is time to send emails)
                }
                catch (Exception ex)
                {
                    //handle exception
                }
                Sleep(sleeptime); 
            }
        }
    }


    private static void Stop()
    {
        stopProcessing = true;
    }
南城追梦 2024-12-16 08:36:07

除了@Peer 的建议之外,根据您网站的流量以及计时的准确程度,还必须有另一种选择,那就是让一些代码在每个请求上运行。该代码将检查定时活动上次运行的时间(通过检查数据库或其他方式),然后如果今天的发送时间已过且尚未发送,则运行发送过程。否则你就照常进行。

当然,您需要生成一个新线程来完成这项工作,这样您就不会在发送电子邮件时阻止用户请求。

此方法的缺点是它依赖于有人点击您的网站来触发。此方法的优点是无需任何特殊代码即可稍后运行。使用计时器的方法将在第一次检查时需要额外的代码,基本上执行此处所做的操作,以检查启动时是否发生了今天的运行。

我要再说一遍,我认为这不是最好的方法,但如果绝对必须通过 ASP.NET 网页,那么这可能是您最好的选择。

不过,如果允许您使用 ASP.NET 中没有的其他代码,那么您可以编写一个应用程序,该应用程序将在正确的时间作为您网站上的特殊页面,将计时器代码保留在您的计算机(或服务器或任何)。该特殊页面将使用某种安全措施来确保只有您可以访问它(例如密码保护或类似的),并且当您点击该页面时,它会执行电子邮件操作。这样,电子邮件代码位于 ASP.NET 页面中,但实际的调度代码位于更合理的位置。

这当然也可能不是一个选择。

Depending on the volume of traffic to your site and how accurate your timing must be another option in addition to @Peer's suggestion is to have some code that runs on each request. That code will check when the timed activity last ran (by checking in database or something) and then if it is gone today's time to send and it hasn't sent yet then you run your send procedure. Otherwise you go on as normal.

Of course you'll want to spawn a new thread to do the work so you don't hold up the user request while it does the e-mail sending.

Cons of this method are that it relies on somebody hitting your site to trigger. Pros of this method is that it can run late without any special code. The method of using timers will require additional code on first check to basically do what is done here to check if today's run has occured when you start it.

I will say again that I don't think this is the best way but if it absolutely has to be via ASP.NET web pages then this might be your best bet.

Though if you are allowed to have other code not in ASP.NET then you could write an app that will go to as special page on your website at the right time, keeping the timer code on an app on your computer (or a server or whatever). the special page will use some kind of security to ensure only you can access it (eg password protection or similar) and when you hit the page it does the e-mail stuff. This way the emailing code is in your ASP.NET pages but the actually scheduling code is in a more sensible location.

This may of course not be an option either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文