My motivation for this article is my beautiful fiancé. She lives in a different state and wants me to email her everyday. Being a "GEEK" I find it difficult to email her regularly. So, I thought that why not use my programming skills to make an application that emails her daily. The idea is that you can write emails for one week on a single day and schedule it to be sent everyday. This way you will never forget to send email and your fiancé or girl friend will also be happy.
My motivation for this article is my beautiful fiancé. She lives in a different state and wants me to email her everyday. Being a "GEEK" I find it difficult to email her regularly. So, I thought that why not use my programming skills to make an application that emails her daily.
The idea is that you can write emails for one week on a single day and schedule it to be sent everyday. This way you will never forget to send email and your fiancé or girl friend will also be happy.
My application is a simple C# console application. I chose console application since it creates an .exe file which can easily be run by Windows Scheduler. The application is divided into two executable files.
AzamSharpAddEmail.exe is used to add a new email to the database (The database only contains one table tblEmail. The database script is added in the zip folder).
ScheduleEmails.exe contain methods to send the email and also to make an entry in the file which is used to keep track of the time email was sent.
Code for AzamSharpAddEmail.exe:
There is only one method "AddNewEmail" which is used to add new email to the database. I have made database connection string in the class file you should use database connection securely. Here is a small code snippet from "AddNewEmail" method:
|Console .WriteLine( "Enter to email address:" );
string toEmail = Console .ReadLine();
Console .WriteLine( "Enter subject:" );
string subject = Console .ReadLine();
// If you use Console.ReadLine() than it will only take 256 characters as input
// since the body of the email is more than 256 characters hence I configure the Console
// to take longer inputs
byte  bytes = new byte ;
Stream inputStream = Console .OpenStandardInput(bytes.Length);
Console .SetIn( new StreamReader (inputStream));
Console .WriteLine( "Enter the body of the email" );
string body = Console .ReadLine();
After taking input from the console we simply write the input to the database.
|SqlCommand myCommand = new SqlCommand( "usp_InsertEmail" ,myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add( "@To" ,SqlDbType.NVarChar,100);
myCommand.Parameters[ "@To" ].Value = toEmail;
myCommand.Parameters.Add( "@Subject" ,SqlDbType.NVarChar,200);
myCommand.Parameters[ "@Subject" ].Value = subject;
myCommand.Parameters.Add( "@Body" ,SqlDbType.NVarChar,3000);
myCommand.Parameters[ "@Body" ].Value = body;
Console .WriteLine( "New Email has been inserted" );
catch ( Exception ex)
Code for ScheduleEmails.exe:
Now let's see that how we send the emails. "ScheduleEmails" contains two methods "SendEmail" and "WriteToFile".
SendEmail method is executed first which connects to the database and read the email which has been marked unread. It picks up the first row from the database which contains the body of the email and also emailTo address and sends email to the receiver.
emailTo = reader[ "To" ] as String ;
emailSubject = reader[ "Subject" ] as String ;
emailBody = reader[ "Body" ] as String ;
MailMessage mail = new MailMessage();
mail.To = emailTo;
mail.From = "YouremailAddress@something.com" ;
mail.Subject = emailSubject;
mail.Body = emailBody;
mail.Priority = MailPriority.High;
Console .WriteLine( "Preparing to send email...." );
After the email is sent we make an entry in the text file which notes the time and date the email was sent.
|private static void WriteToFile()
StreamWriter sw = File .AppendText( @"C:\EmailInfo.txt" );
sw.WriteLine( "Email was sent on" + DateTime .Now);
catch ( Exception ex)
Everything is done now the question is how do we schedule this .exe file to run everyday at specified time. This is very easy if you are using "Windows Scheduler".
Schedule email using Windows Scheduler:
Open control panel and you will see an icon "Scheduled Tasks". Double click on it and it will launch a wizard. The wizard is pretty straight forward just remember that if will ask you that which application you want to schedule than simply point to the ScheduleEmails.exe file. You may also have to enter username and password in order to schedule a task.
In the last I would like to dedicate this article to my beautiful fiancé, Naila Sheikh. And I hope she does not read this article :).