How to send mail automatically for every five minutes using C#?

This article explain how to send mail automatically using window service, Concepts are involved for sending mail automatically are SMTP Server for sending mails, Window service used to mail automatically, Event log is used to see whether the window service is working, below explain detail.
1. Window Service
2. Event Log
3. SMTP Client
4. Timer Concepts
5. Steps to Create Automatic Mail Service
6. Create Windows Service Deployment project
7. Instal and Un-install the windows servie project
8. Start and Stop your service
9. View your log files

1.Window Service


Windows Service is used to create a application to run in a windows sessions. windows service can automatically run,  restart or paused when the system boots , we do not show any user interface for doing the window service.
In .Net framework support two type of windows service. window service can run in a single process to be create in Win32ownprocess and the service can share the process to be create in win32shareprocess. we can retrieve the information of the process using servicetype property.
They are two overrides method are used to Start and Stop your windows service.
1. Start  method
protected override void OnStart(string[] args)
   //Your coding
2. Stop Method
protected override void OnStart(string[] args)
   //Your coding
2.Event Log

Eventlog class used to create or accessing the windows event logs. Administration privileges need to write to an event log for every logs.EventLog,class method are used to read from existing logs, write entries to logs and create a logs  or delete event sources. 

Check whether event log exisits in system using SourceExists method, if not created already create eventlog using CreateEventSource method and write to the event Source
Create New EventLog
if (EventLog.SourceExists("AutoMail"))
Write the information in existing logs
    eventLog1.Source = "AutoMail";
    eventLog1.Log = "Mails";

3.SMTP Server

The SMTP Server Class is used to Send Mail from a SMTP Server. .Net Framework 2.0 later supports the SMTP Server class from  System.Net.Mail namespace. 
It System.Net.Mail namespace supports three class
a) MailMessage
b) MailAddress.and
c) Attachments
Mail Message is used to describe the messages.
Mail Address is used to define theh sender and recipiants.
Attachments is used to attach the file along with the mail.

MailMessage mailMsg = new MailMessage();
MailAddress mailAddress = null;
mailAddress =new MailAddress(;
mailMsg.From = mailAddress;
mailMsg.Subject = "Automatic mailing";
mailMsg.Body = "Tomorrow Meeting at 6.30PM";
SmtpClient smtpClient = new SmtpClient("", Convert.ToInt32(25));
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
smtpClient.Credentials = credentials;
Here i have used  ipaddress and Port number is our SMTP server. So change the ip and port according to your SMTP Server configuration.

4. Timer Concepts

.Net providers timer component is a server-based timer allows you to specify interval and the elapsed event is raised in your application. This event to provide regular processing. while create a service that uses a timer to periodically , it will check the server is running or not.
While service could attempt to restart the server, timer component raises the elapsed event based on the value of the interval property.
System.Timers.Timer time = new System.Timers.Timer();
time.Interval = 300000;
time.Elapsed += time_elapsed;


public void time_elapsed(object sender, ElapsedEventArgs e)