At a times there becomes a requirement to send automatic mail to a particular no
of clients in our web projects. Recently working on a web project which
basically deals with Ticket issue of movies. In this project I was asked to send
an automatic mail to the person in charge after the movies 1 hour ie if the show
time is 2'o clock then the automatic mail should be sent at 3'o clock and
finally it should logout of the application.
The part of automatic sending mail can be done using a timer control from the ajax
toolkit also but the main drawback is that you always have to check whether the
system time is equal to 3'o clock or not so it unnecessarily waste the system's
resources if we directly write the code in our aspx.cs file. So in order to
increase the performance I've used BackgroundWorker class of
The BackgroundWorker class allows you to run an operation on a separate,
dedicated thread. Time-consuming operations like downloads and database
transactions can cause your user interface (UI) to seem as though it has stopped
responding while they are running. When you want a responsive UI and you are
faced with long delays associated with such operations, the BackgroundWorker
class provides a convenient solution.
To execute a time-consuming operation in the background, create a
BackgroundWorker and listen for events that report the progress of your
operation and signal when your operation is finished. You can create the
BackgroundWorker programmatically or you can drag it onto your form from the
Components tab of the Toolbox. If you create the BackgroundWorker in the Windows
Forms Designer, it will appear in the Component Tray, and its properties will be
displayed in the Properties window.
To set up a background operation, add an event handler for the DoWork event.
Call your time-consuming operation in this event handler. To start the
operation, call RunWorkerAsync. To receive notifications of progress updates,
handle the ProgressChanged event. To receive a notification when the operation
is completed, handle the RunWorkerCompleted event.
Enough of this theory, we'll go into the example just for testing purpose I've
created a small but effective example. Following is the design code for the
Since background worker will create a different thread for executing the code of
ours ie we can say that the code will be executed in an asynchronous manner, so
we need to define the Async property of our page to true.
<%@ Page Language="C#" AutoEventWireup="true" Async="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send Mail" /></div>
Following is the source code for the same.
for backgroundworker class
public partial class _Default : System.Web.UI.Page
bw = new
bw.WorkerSupportsCancellation = true;
bw.WorkerReportsProgress = false;
public void SendMail()
MailMessage msg =
msg.From = new
msg.Body = "Testing the automatic mail";
msg.IsBodyHtml = true;
msg.Subject = "Movie Data";
SmtpClient smt =
smt.Port = 587;
smt.Credentials = new NetworkCredential("email@example.com",
smt.EnableSsl = true;
string script =
"<script>alert('Mail Sent Successfully');self.close();</script>";
DateTime current_time =
current_time = current_time.AddSeconds(10);
if (current_time ==
In this code after the user clicks the button the mail will be send
automatically after 10 seconds to the specified email id.
Following is the output for the same.