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.

 Introduction:

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.

Email Application:

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: 

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:

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 [2000];

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;

try

{

myConnection.Open();

myCommand.ExecuteNonQuery();

myConnection.Close();

Console .WriteLine( "New Email has been inserted" );

Console .Read();

}

catch ( Exception ex)

{

Console .WriteLine(ex.Message);

}

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.


while (reader.Read())

{

emailTo = reader[ "To" ] as String ;

emailSubject = reader[ "Subject" ] as String ;

emailBody = reader[ "Body" ] as String ;

}

try

{

MailMessage mail = new MailMessage();

mail.To = emailTo;

mail.From = "[email protected]" ;

mail.Subject = emailSubject;

mail.Body = emailBody;

mail.Priority = MailPriority.High;

Console .WriteLine( "Preparing to send email...." );

SmtpMail.Send(mail);

 

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()

{

try

{

StreamWriter sw = File .AppendText( @"C:\EmailInfo.txt" );

sw.WriteLine( "Email was sent on" + DateTime .Now);

sw.Close();

}

catch ( Exception ex)

{

Console .WriteLine(ex.Message);

}

}

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.

Note:

If you have any trouble sending emails please than check out my article http://www.azamsharp.net/SendingEmails.htm. For more help in sending emails visit www.systemwebmail.com

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 :).