0

In my ASP.net website i have done with sending email to users when they register their self but what i want that when a user register from my website it will automatically send email after 5 minutes instead of immediate sending after registration. Here is my simple code :

private void SendEmail()
        {

                SmtpClient smtpClient = new SmtpClient();
                MailMessage mailMsg = new MailMessage();
                MailAddress fromAddress = new MailAddress("test@test.com");
                smtpClient.Host = "mail.test.com";
                smtpClient.Credentials = new NetworkCredential("test@test.com", "test");
                smtpClient.Port = 25;
                mailMsg.From = fromAddress;
                mailMsg.To.Add(emailid);
                mailMsg.Subject = "You are successfully registered";
                mailMsg.Body = myString.ToString();
                mailMsg.IsBodyHtml = true;
                smtpClient.Send(mailMsg);
                System.Threading.Thread.Sleep(500000);

}

I used System.Threading.Thread.Sleep(50000) to wait for 5 minutes but this did not help me out to get my exact needs.

I am thinking that i need to set a flag in database table against every registered user email and i will make a webservice which will execute after 5 minutes to see which email addresses needs to be send an email . Is there any other little fast way to do this?

Ammar Asjad
  • 2,920
  • 6
  • 29
  • 42
  • 1
    Well no, that's 50 seconds not 5 minutes. But you really don't want to tie up a thread like that. Some ideas on [this old question](http://stackoverflow.com/questions/542804/best-way-to-run-scheduled-tasks). – Rup Nov 27 '14 at 23:41
  • What you want is to write some kind of service that will poll your database for users who registered 5 minutes before it runs and sends them the email. You then hook it up to http://getcloudscheduler.com/ – levelnis Nov 27 '14 at 23:45
  • This question gets asked over & over. (how to return the page to the user and keep doing something afterwards on the server) .NET 4.5 has a solution, ref: http://www.hanselman.com/blog/HowToRunBackgroundTasksInASPNET.aspx you can also use opensource HangFire: https://github.com/HangfireIO/Hangfire – MatthewMartin Nov 27 '14 at 23:52

1 Answers1

1

Store a registration email, and registration date in database (or in text file).

Make a simple different applicication which do the following:

-Checks the current date to the registration date plus 5 minutes.
-if its bigger, send out the email
-if the sending was success, remove the entry from the db

Build this application, and call the exe in every 4-5 minutes via cronjob or windows scheduler

Krekkon
  • 1,349
  • 14
  • 35