Here I have created sample.
Global.asax
<script RunAt="server">
private static System.Threading.Timer _timer;
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
SetUpTimer();
}
private void SetUpTimer()
{
TimeSpan timeToGo = DateTime.Now.AddDays(1).AddHours(9) - DateTime.Now; //timespan for 09:00AM tommorrow
// TimeSpan timeToGo = DateTime.Now.AddDays(1) - DateTime.Now; //timespan for 00:00AM tommorrow
//TimeSpan timeToGo = DateTime.Now.AddMinutes(2) - DateTime.Now; //timespan for 02 Minutes from Now
_timer = new System.Threading.Timer(x => SendEmails(), null, timeToGo, new TimeSpan(1, 0, 0, 0));
}
public void SendEmails()
{
string subject = "Happy Birthday";
string body = "Happy Birthday {0},<br /><br />Thanks.";
List<Employee> employees = new List<Employee> {
new Employee{
Id = 1,
Name = "David",
Email = "David@gmail.com",
Dob = DateTime.Parse("2015-12-31")
},new Employee{
Id = 2,
Name = "Kevin",
Email = "Kevin@gmail.com",
Dob = DateTime.Parse("2015-12-25")
},new Employee{
Id = 3,
Name = "Jhon",
Email = "Jhon@gmail.com",
Dob = DateTime.Parse("2015-12-31")
}
};
List<Employee> badyEmployees = employees.Where(employee => employee.Dob.ToShortDateString() == DateTime.Now.ToShortDateString()).ToList<Employee>();
System.Threading.Tasks.Parallel.ForEach(badyEmployees, employee =>
{
SendEmail(employee.Email, subject, string.Format(body, employee.Name));
});
}
private bool SendEmail(string recipient, string subject, string body)
{
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage("sender@gmail.com", recipient);
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = true;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "sender@gmail.com";
NetworkCred.Password = "<password>";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
return true;
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public DateTime Dob { get; set; }
}
</script>
I hope this will help you out.