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
System.ComponentModel.Component namespace.
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
same.
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" >
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send Mail" /></div>
- </form>
- </body>
- </html>
Following is the source code for the same.
- using System;
- using System.Data;
- using System.Configuration;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.ComponentModel;
- using System.Net;
- using System.Net.Mail;
- using System.Threading;
- public partial class _Default : System.Web.UI.Page
- {
- BackgroundWorker bw;
- protected void Page_Load(object sender, EventArgs e)
- {
- bw = new BackgroundWorker();
- bw.DoWork+=new DoWorkEventHandler(bw_DoWork);
- bw.WorkerSupportsCancellation = true;
- bw.WorkerReportsProgress = false;
- }
- public void SendMail()
- {
- MailMessage msg = new MailMessage();
- msg.From = new MailAddress("[email protected]");
- msg.To.Add("[email protected]");
- msg.Body = "Testing the automatic mail";
- msg.IsBodyHtml = true;
- msg.Subject = "Movie Data";
- SmtpClient smt = new SmtpClient("smtp.gmail.com");
- smt.Port = 587;
- smt.Credentials = new NetworkCredential("Your Gmail Id", "Your Password");
- smt.EnableSsl = true;
- smt.Send(msg);
- string script = "<script>alert('Mail Sent Successfully');self.close();</script>";
- this.ClientScript.RegisterClientScriptBlock(this.GetType(), "sendMail", script);
- }
- public void bw_DoWork(object sender, DoWorkEventArgs e)
- {
- SendMail();
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- DateTime current_time = DateTime.Now;
- current_time = current_time.AddSeconds(10);
- Thread.Sleep(10000);
- if (current_time == DateTime.Now)
- {
- bw.RunWorkerAsync();
- }
- }
- }
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.