Introduction
Every office maintains its own system to maintain the entry time of its employees in order to make sure they are maintaining office hours properly. For my case, I need to send an entry mail composed with my formal signature and the time. At my work, I usually forget to send the entry mail to the boss. So there may be some confusion whether I have attended office today on time or not. So I decided to create an application that will help me to solve my daily problem. Today, I am going to describe my way to do this.
Background
I usually forget to send my entry mail in due time. For this reason, I have decided to develop an application to do this task for me. It is actually easier for programmers to develop an app other than to memorize to do a task. That's why the task scheduler idea came up. I have also made an installer for programmers' easy use, if any one wants to use it without having programming knowledge. So let's start.
Using the Code
Eventually, I deployed all possible ways to make it user friendly. I have kept in mind that non-programmers may use this application. So I developed it in that way. In my first section, I will describe the programming methodology that I have used in my application and in the second section, I will describe how to use my application.
First Section
First of all, we need to understand the scenario of the application we are developing.
Here it is:
- We need to develop an application that will send mail automatically when the computer starts.
- We need to take input from user: the mailing address (To), network credentials and of course, the user's mail address.
- Then, we can add some signature or other necessary information that we want to send with the mail.
- Then, we will send the mail.
Now the scenario is clear about what we are going to do and I am now describing how and why we are going to do this with detailed elaboration along with the code segment.
We will start from the second point, that is, "We need to take input from user: the mailing address (To), network credentials and of course, the user's mail address. "
So here are the situations that may arise:
- User can put wrong mail address, so we need to verify that the email address maintains the standard format.
- Here in my case, I am taking name, email, phone and designation as inputs from the user. So I need to validate the phone number as well. At this moment, I have only allowed U.S. phone number as valid. So if you are from another country, kindly change the code to validate your phone number.
Here is the code segment for Email validation:
if (!tbxEmail.Text.Equals(string.Empty))
{
try
{
new System.Net.Mail.MailAddress(tbxEmail.Text);
}
catch (Exception ex)
{
MessageBox.Show("Email Not Valid" + ex);
return;
}
}
Here is the code for Phone Number validation:
if (!tbxPhone.Text.Equals(string.Empty))
{
string[] numbers = { tbxPhone.Text };
const string sPattern = @"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}";
foreach (string s in numbers)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(s, sPattern))
{
MessageBox.Show("Not a valid Phone Number");
return;
}
}
}
If you look at the above code, you will see that I have commented out the pattern for phone number of Bangladesh. If you need to assign phone number of other countries, please add a regular expression for your phone number. That will work fine, or if this causes any problem, you can simply modify the text file of you D: drive and put your required information there (but not more that 4 paragraphs and also you have to maintain the format of the code that is name, email, phone & designation). You will be able to change everything except the mail address, you need to keep the mail address in the 2nd paragraph to perform the task properly.
So we have taken inputs from user after validating all the above criteria. Now we need to save the file. I have saved the file as "info.txt" in the D: drive of your computer. When you will run this project, it will create two files.
The first one is named "info.txt" for holding information of the user and the second one is named "date.txt" for checking if the mail has already been sent for the day.
Now we need to configure the mailing address of your application.
- We need to put mailing address (To) and network credentials.
- Mailing address (To) is required to send the mail to a particular address. That is our "To" of the typical mail protocol. Network credentials are required because we needed to send mail to a varieties of mail addresses. That's why we need to go through a mail address that I have access to. It maintains the format like below:
var cred = new NetworkCredential(_credentialMail, _credentialPassword);
var mailClient = new SmtpClient("smtp.gmail.com", 587);
It means we are using gmail as SMTP client and the network credentials that you will provide in _credentialMail
& _credentialPassword
.
For example: Let's have 3 mail addresses like below:
Address 1: x@x.com
Address 2: y@y.com
Address 3: z@gmail.com Password:12345
Now think we want to send an email to <y@y.com>
from <x@x.com>
. So we need to put it like below to work for this example:
To: y@y.com
Network Credential Mail Address: z@gmail.com
Password of the Above Mail Address: 12345
We have saved this information to a text file named CredentialInfo.txt to the D: drive. Here, you need to know that I have only configured the credentials for gmail. So the Network Credential Mail Address must be a gmail address.
Here is the method that does the work to create the files:
int CreateFile()
{
if (!File.Exists(@"D:\date.txt"))
{
try
{
using (File.Create(@"D:\date.txt"))
{
}
TextWriter dateS = new StreamWriter(@"D:\date.txt");
dateS.WriteLine(DateTime.Now.ToShortDateString());
dateS.Close();
_createfile = 1;
}
catch (Exception ex)
{
_createfile = 0;
}
}
if (!File.Exists(@"D:\info.txt"))
{
try
{
using (File.Create(@"D:\info.txt"))
{
}
_createfile = 1;
}
catch (Exception ex)
{
_createfile = 0;
}
}
if (File.Exists(@"D:\info.txt") && File.Exists(@"D:\date.txt"))
{
_createfile = 1;
}
return _createfile;
}
We have made the settings for our application!! Wow. Now we need to send the mail. Here is the code block to send the mail.
try
{
TextReader tw = new StreamReader(@"D:\CredentialInfo.txt");
_toMail = tw.ReadLine();
_credentialMail = tw.ReadLine();
_credentialPassword = tw.ReadLine();
message.From = new MailAddress(_mail);
if (_toMail != null) message.To.Add(_toMail);
message.Subject = "Entry Time ( " + DateTime.Now.ToShortDateString() + " ) " +
DateTime.Now.ToShortTimeString();
message.BodyEncoding = Encoding.UTF8;
message.Priority = MailPriority.Normal;
var cred = new NetworkCredential(_credentialMail, _credentialPassword);
var mailClient = new SmtpClient("smtp.gmail.com", 587);
mailClient.EnableSsl = true;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = cred;
mailClient.Send(message);
MessageBox.Show("Entry Mail Sent Successfully");
}
You can see that we are reading the network credential information from the text file. So we need to put valid information in the text file to make it work.
I have also saved the information to a database. Here is the method to do this.
void insert_database()
{
try
{
_con.Open();
_cmd.Connection = _con;
_cmd.CommandText = @"INSERT INTO MailInfo (Name,Email,Phone,Designation,MailDate,MailTime)
VALUES ('" + _name + "','" + _mail + "','" + _phone + "','" + _des + "','" +
DateTime.Now.ToShortDateString() + "','" + DateTime.Now.ToShortTimeString() + "')";
_cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show("Inserting Error in database " + ex);
}
}
Here is the database table design:
Second Section
Please follow the steps below to configure the application.
Step 1: Run the project.
Step 2: Settings-> Registration -> Fill the text boxes with your information. Here is an example.
Here are some validations that you need to be concerned about. Follow the U.S. phone format to input the phone number. Example XXX-XXX-XXXX.
Also, you need to worry about the mail address format:
When you will input information properly, then the registration success window will appear.
Step 3: Settings-> Configure Mailing Address -> Fill the text boxes with your information. Here is an example.
Step 4: Go to the D drive of your computer and change the date to the previous date replacing the current date.
Step 5: Press the Send Mail button. The mail will be sent to the address you have entered to the mailing address. You will find a logo downloaded to your drive. You can also produce your own logo or your company logo. To do this, you need to put the link here.
if (!File.Exists(@"D:\desme_logo.gif"))
{
const string localFilename = @"D:\desme_logo.gif";
using (var client = new WebClient())
{
client.DownloadFile("https://dl.dropbox.com/u/29122962/desme_logo.gif", localFilename);
}
}
Change the link of the dropbox and put your own link and then put the file name everywhere I used:
@"D:\desme_logo.gif"
I have also created a setup project. You will find everything if you download the project.
It you want to send mail when the computer starts, then you need to do the following:
Step 1: Go to project folder -> open program.cs -> add Application.Run(new auto_mail());
replacing Application.Run(new Home());
Note: There are two separate projects. Both of them are working. You need to separate the two projects by replacing Application.Run(new auto_mail()); with Application.Run(new Home());
or replacing Application.Run(new Home()); with Application.Run(new auto_mail());
To send automail, do the last one because by default, I have coded the first one.
Step 2: Build the project -> then rebuild the setup project (automail).
Step 3. Right click on the setup project -> select install -> Finish the installation.
Step 4: Goto your desktop -> you will see automail.exe run the EXE.
Step 5: If it prompts that you are not registered, then register using the procedure I mentioned earlier.
Step 6: Close the window -> change the date of the date.txt file as earlier and put the previous date of today's date -> save the file.
Step 7: Run the automail.exe again. Then the mail will be sent off now with the necessary information and the information will be saved in the database and you will see the window below if everything goes right " />
If you want to send out your mail when the computer starts, then follow the steps below:
Step 1: My computer->right click -> manage.
Step 2: Double click on Task Schedular -> right click Task Schedular Library -> create Task. Then follow the screenshots.
Step 3: Click on Tab Triggers- > then new. Then select the following as the input of the image. Set Active and expired date as you like to trigger the event. and put some delay if you are using modem to connect your computer to the internet.
Step 4: Then click on action tab and do the following. Browse the automail.exe on your desktop.
Step 5: We are done here. You can check the task by right clicking on the task and then Run the task.
Hope the project will help you.
Happy coding ...
History
- 10th February, 2013: Initial version