.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Code Snippets >> Email >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Sending e-mail on some Schedule date/time

Posted By:Ashutosh Jha       Posted Date: June 30, 2014    Points: 40    Category: Email    URL: http://www.dotnetspark.com  

Here i will explain how one can send an auto generated emails to some scheduled time. We often need this functionality when we have to send mail or messages to clients about information or updates.

In my earlier articles i have explained-

Here i will explain how to schedule the emails notifications to some particular date or time.

For this we need to have some coding with Global.ascx file. If your site does not have global.ascx file, first create it and then add the following code-
<%@ 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();

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.



And now simply design a page in which you have written email code. it will look something like this-
StreamReader reader = new StreamReader(Server.MapPath("~/html page")); //if want to else no need
string readFile = reader.ReadToEnd();
string myString = "";
myString = readFile;

MailAddress fromMail = new MailAddress("email");

Msg.CC.Add(new MailAddress(clientEmail(getClientId(cn.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;

Msg.Subject = "Send Mail with scheduler";
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 = 30000;


Thats all. Set your time interval as per your need.


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend