mazhar 124
mazhar 124

Reputation: 123

How to send email automatically at a particular time every day

How to send e-mail automatically at 11am every day using ASP.NET, C#, Webforms?

I am doing on button click to send mail. My requirement is every day, at 11 am, that mail should be sent automatically. Is this possible using C# code?

protected void SendEmail(object sender, EventArgs e)
{
   using (StringWriter sw = new StringWriter())
   {
       using (HtmlTextWriter hw = new HtmlTextWriter(sw))
       {
           gv_TotalAllReg.RenderControl(hw);
           StringReader sr = new StringReader(sw.ToString());
           MailMessage mm = new MailMessage("[email protected]", "[email protected],[email protected]");

           mm.Subject = "Daily Report";

           mm.Body = "Dear Customer, <br><br>Applications received as on: " + ViewState["date"] + ".<hr />" + sw.ToString() + "<br/>Thanks,<br>Support Team.";

           mm.IsBodyHtml = true;
           mm.CC.Add("[email protected]");

           SmtpClient smtp = new SmtpClient();
           smtp.Host = "smtp.gmail.com";
           smtp.EnableSsl = true;

           System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
           NetworkCred.UserName = "[email protected]";
           NetworkCred.Password = "n0";
           smtp.UseDefaultCredentials = true;
           smtp.Credentials = NetworkCred;
           smtp.Port = 587;

           smtp.Send(mm);

           lbl_msg.Visible = true;
       }
   }
}

Below is my button code.

<asp:Button ID="btnSendEmail" runat="server" Text="Send email" OnClick="SendEmail" class="btn btn-md btn-danger pull-right" />

When I will button click then gridview data is sending to emails. But I am trying like below concept.

   If(time==11am)
   {
     //send mail
   }

I search lot but got Windows Service related example but I am not getting that type of concept. Is there any other ways.

Upvotes: 3

Views: 12681

Answers (2)

Gaste
Gaste

Reputation: 325

A web forms applications is the wrong place for scheduled tasks for several reasons. First, web forms it is based on the request-response paradigm of http. This means that server code is triggered by http requests, e.g. when a user browses a page or clicks on a button. Theoretically, you could implement an infinite loop that checks for the condition (i.e. utilizing sleep or even a timer) and then sends the message, which is executed by SendMail. However, this is a bad idea as the IIS application pool your webforms application is running in might be recycled.

As an alternative, you can

  • create a console application that is performing the mail send task and execute it using the Windows task scheduler, or
  • create a windows service that performs the send mail task, or
  • use any other task scheduling utility to execute a console app or call a web service.

Upvotes: 2

Ygalbel
Ygalbel

Reputation: 5519

You have to use a job scheduler library.

Use hangfire or Quartz.net

You will be able to schedule a job a specific time.

With hangfire it will be like this:

BackgroundJob.Schedule(
    () => SendMail(), 
    new DateTime(2017, 27, 03, 11, 00, 00));

Upvotes: 6

Related Questions