How to send mail or messages on specific time or Schedule time
We often need this functionality when we have to send mail or messages to clients about information or updates of our system or whatever.
e.g. School management system send daily,monthly or weekly reports by mail to their students.
In this article i will give logic for how to send mail or messages in a specific time or Scheduled time to clients using asp.net
This is simple,We have do some coding in Global.ascx file
if your website or web appication doesn't contain this file then simply added this Global.ascx file.
you have to simply follow this steps which is described in this article.
Actually i will give a link for that,
http://www.aspdotnet-suresh.com/2011/05/how-to-add-globalasaxcs-file-in-aspnet.html
Now Put this code into your Global.ascx file.
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Timers" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
System.Timers.Timer myTimer = new System.Timers.Timer();
// Set the Interval to 10 seconds (10000 milliseconds).
myTimer.Interval = 10000;
myTimer.AutoReset = true;
myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
myTimer.Enabled = true;
}
public void myTimer_Elapsed(object source, System.Timers.ElapsedEventArgs e)
{
clsScheduleMail objScheduleMail = new clsScheduleMail();
objScheduleMail.SendScheduleMail();
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
And now simple put code into your Default.aspx.cs file for send a mail or message means implement code for send message or mail
Here i will give a simple for that.
StreamReader reader = new StreamReader(Server.MapPath("~/HTMLPage1.htm"));
string readFile = reader.ReadToEnd();
string myString = "";
myString = readFile;
MailAddress fromMail = new MailAddress("ketanitaliya16@gmail.com");
Msg.CC.Add(new MailAddress(clientEmail(getClientId(lblclientname.Text))));
myString = myString.Replace("$$Client_Name$$", ev.Client_Name);
myString = myString.Replace("$$Event_Name$$", ev.Event_Name);
myString = myString.Replace("$$Activation Code$$", ev.Activation_Code);
Msg.From = fromMail;
// Subject of e-mail
Msg.Subject = "Send Mail with HTML File";
Msg.Body = myString.ToString();
Msg.IsBodyHtml = true;
var smtp = new System.Net.Mail.SmtpClient();
{
smtp.Host = "localhost";
smtp.Port = 25;
smtp.EnableSsl = false;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential(fromMail.ToString(), "123456789");
smtp.Timeout = 20000;
}
smtp.Send(Msg);
reader.Dispose();
So with this coding a mail of information is send to clients on a specific fixed time.
You can set time as per your Requirement.
I hope this will help you,
Thanks&Regards
Ketan Italiya