Automatically send Birthday email using C#.Net
In order to automatically send Birthday email daily at particular time, a Windows Service will be created which will run daily at the particular time and will automatically send Birthday emails using C#.Net.
In this article the Windows Service will execute a Task daily at a specific time. The Task is to fetch all records of Students who have a Birthday today.
Such students will receive an email containing Birthday wishes automatically by the Windows Service.
Config:-
<appSettings>
<add key ="Mode" value ="Daily" />
<add key ="IntervalMinutes" value ="1" />
<add key ="ScheduledTime" value ="17:43" />
</appSettings>
<connectionStrings>
<add name="constr" connectionString="Data Source=.\SQL2008R2;Initial Catalog=StudentsDB;integrated security=true" />
</connectionStrings>
We will need to modify the SchedularCallback event handler in order to automatically fetch records of Students who have a Birthday and send them emails.
First a query is executed to fetch all Student records by matching the Day and Month of their Birthdate with the Day and Month of the Current Date respectively.
Then a loop is executed and one by one an email is sent to each Student.
Once the Task is accomplished the Windows Service enters resting mode and will again perform the same task after 24 hours at the specified time.
private void SchedularCallback(object e)
{
try
{
DataTable dt = new DataTable();
string query = "SELECT Name, Email FROM Students WHERE DATEPART(DAY, BirthDate) = @Day AND DATEPART(MONTH, BirthDate) = @Month";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Day", DateTime.Today.Day);
cmd.Parameters.AddWithValue("@Month", DateTime.Today.Month);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
}
}
}
foreach(DataRow row in dt.Rows)
{
string name = row["Name"].ToString();
string email = row["Email"].ToString();
WriteToFile("Trying to send email to: " + name + " " + email);
using (MailMessage mm = new MailMessage("sender@gmail.com", email))
{
mm.Subject = "Birthday Greetings";
mm.Body = string.Format("<b>Happy Birthday </b>{0}<br /><br />Many happy returns of the day.", name);
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
credentials.UserName = "sender@gmail.com";
credentials.Password = "<Password>";
smtp.UseDefaultCredentials = true;
smtp.Credentials = credentials;
smtp.Port = 587;
smtp.Send(mm);
WriteToFile("Email sent successfully to: " + name + " " + email);
}
}
this.ScheduleService();
}
catch (Exception ex)
{
WriteToFile("Simple Service Error on: {0} " + ex.Message + ex.StackTrace);
//Stop the Windows Service.
using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("SimpleService"))
{
serviceController.Stop();
}
}
}
Comments
Post a Comment