I have a piece of code that I need to be run automatically in the background regardless of the page I am viewing. For example if I am in the homepage or about page of a website I want a piece of code to still run automatically. To be precise I want to send a email notification every 30 minutes from the email class that I have made. I am aware that similar thing can be done via windows service but I want the code to be in the website.
public class Email
{
string emailFrom = "[email protected]";
string password = "yourpassword";
string smtpServer = "smtp.gmail.com";
int port = 587;
public void sendEmail(string emailTo, string subject, string body)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress(emailFrom);
msg.To.Add(emailTo);
msg.Subject = subject;
msg.Body = body;
SmtpClient sc = new SmtpClient(smtpServer);
sc.Port = port;
sc.Credentials = new NetworkCredential(emailFrom, password);
sc.EnableSsl = true;
sc.Send(msg);
}
}