我的asp.net网站每天自动在12.00Am发送邮件 [英] Send mail everyday automatically at 12.00Am from my asp.net website

查看:97
本文介绍了我的asp.net网站每天自动在12.00Am发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友


我正在使用C#使用ASP.NET开发一个网站.在该站点中,我想每天自动发送邮件12.00Am.我正在使用以下代码通过Thread Concept发送邮件

Hello Friends


I am developing one website with ASP.NET using C#. In that site I want to send mail everyday 12.00Am Automatically. The following code i am using to send mail with Thread Concept

void mailsend()
{
        
        while (true)
        {
            try
            {
                i = 1;

                if ((DateTime.Now.Hour == 13) && (DateTime.Now.Minute == 00) && (DateTime.Now.Second == 00))
                {

                    mailtosend.To.Add(names[0]);
                    while (names[i]!=null)
                    {
                        //mailtosend.To.Add(names[i]);
                        mailtosend.Bcc.Add(names[i]);
                        i++;
                    }
                    //Response.Write("The id added " +mailtosend);
                    

                        mailtosend.From = new MailAddress("reports@cutchsoft.com");
                       
                        mailtosend.Subject = "Cutch Tele Login Reports as of  " + yesterday;
                        mailtosend.IsBodyHtml = true;
                        mailtosend.Body = htmlbody;
                        SmtpClient sc = new SmtpClient();
                        sc.Host = "mail.cutchsoft.com";
                       
                        sc.Credentials = new System.Net.NetworkCredential("reports@cutchsoft.com", "reports");
                       
                        sc.Send(mailtosend);
                    }
                }//if check
           
            catch (Exception ee)
            {
                //ClientScript.RegisterStartupScript(this.GetType(), "a", "");
            }
        }//while
    }//mail send fun

此处此函数将作为线程函数运行

而此站点在本地系统上运行时邮件正常发送.但是,如果我托管在Web服务器中,则它不会发送邮件.如果有人知道解决方案,请回复我

谢谢您

Here this function will be running as thread function

While this site running in local system mail sending properly. But if i hosted in web server then its not sending mail. If any one know the solution pls reply me

thanking you

推荐答案

网站无意这样做. Web的整个概念基于请求-响应.

您应该为此使用Windows服务,或者编写一个简单的Windows应用程序并安排其在特定时间运行.
Websites are not meant to do this. The whole concept of web is based of request-response.

You should either use a Windows Service to this or write a simple windows application and schedule it run at specific time.


正如其他人所说,Web应用程序不适合这样做.最好的选择是创建一个控制台"应用程序,该应用程序计划每天使用Windows Scheduler运行.当然,您需要在服务器上安装此应用程序.
As others said, a web application is not suitable for doing this. Your best bet is to create a "console" application that is scheduled to run everyday using windows scheduler. You need to install this application on server, of course.


原因是,如果无法访问Web应用程序,则它们会在固定的时间后释放资源(即被销毁).因此,它们不适用于这种类型的时间相关功能,作为类的实例,它不能保证存在.

如前所述,Windows服务更适合于此任务.
The reason is that web applications release resources (ie are destroyed) after a fixed amount of time if they are not accessed. As such they are unsuitable for time-dependant functionality of this type, as an instance of the class it not guaranteed to exist.

As the previous poster said, a windows service is much more suited to this task.


这篇关于我的asp.net网站每天自动在12.00Am发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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