Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
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
Posted
Updated 27-Jan-10 5:50am
v3

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.
 
Share this answer
 
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.
 
Share this answer
 
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.
 
Share this answer
 
Comments
shraddha bhagat 4-Jul-12 7:41am    
also they categorised
Keith Barrow 4-Jul-12 8:29am    
Are you the original poster, your name/account is different but you seem to know about the problem? Categorised or not, a web application is unsuitable for timed jobs such as these. You can get it to work but the solution is harder, less maintainable and makes less sense than either the a Windows service, or a scheduled console app.

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