Click here to Skip to main content
12,632,090 members (26,265 online)
Click here to Skip to main content
Add your own
alternative version

Stats

122.4K views
5.7K downloads
37 bookmarked
Posted

Simple Windows Service which sends auto Email alerts

, 16 Nov 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
Sending Email alerts to your friends saying Good Day

Introduction                                                 

   Sample screenshot Download sourcecode

 Hi,                                                                                  

  This is Ravindra, Implemented a windows service in one of my project and like to place a

 sample Windows service for the very Basic users of .Net.

     First Create a table in Msaccess with Name  'Empdata'

       Sample screenshot


 1.Open Visual Studio.net > Select New Project 

              Sample screenshot


     2.Select Windows Service and give Name as 'GoodDay' and Click Ok.


     3.Right Click the Design and Select Properties and give the Name and ServiceName as 

        'GoodDay' and set AutoLog to 'True'


     4.Drag and drop the OleDbConnection and Select its properties and change its name to

        'conn' and provide the connection String(I provided the Database sample(Ms

         Access)) along with this code.


     5.Right Click the Design and select View Code and

        Select Project > Add Reference > Select System.Web >Press Ok


     6.Now add the following to your code          <FONT color=#0000ff size=2>

          using System.Web.Mail;

          <FONT color=#0000ff size=2>using System.Data.OleDb;


     7.Declare DateTime mdt=DateTime.Now;


     8.    In <FONT color=#0000ff size=2>static <FONT color=#0000ff size=2>void Main()

         Edit the following Code

        ServicesToRun = <FONT color=#0000ff size=2>new System.ServiceProcess.ServiceBase[] { <FONT color=#0000ff size=2>new GoodDay() };


     9. Edit the following code in onstart()

        

DataSet ds = <FONT color=#0000ff size=2>new DataSet();

<FONT color=#0000ff size=2>protected <FONT color=#0000ff size=2>override <FONT color=#0000ff size=2>void OnStart(<FONT color=#0000ff size=2>string[] args)

{

DateTime dt=DateTime.Now;

conn.Open();

OleDbDataAdapter da=<FONT color=#0000ff size=2>new OleDbDataAdapter("select * from Empdata",conn);

da.Fill(ds);

<FONT color=#0000ff size=2>if(dt.ToShortDateString()==mdt.ToShortDateString())

{

<FONT color=#0000ff size=2>foreach(DataRow dr <FONT color=#0000ff size=2>in ds.Tables[0].Rows)

{

mdt=DateTime.Now.AddDays(+1);

String mailtxt="";

MailMessage mm = <FONT color=#0000ff size=2>new MailMessage();

mm.BodyFormat = MailFormat.Html;

mm.To = dr["emp_email"].ToString();

mm.From = "xyz@xyz.com";

mm.Subject="Good Day";

mailtxt = "<font face='verdana' color='#FF9900'><b>"+"Hi "+dr["emp_name"].ToString()+"," + "</b></font><br><br>";

mailtxt=mailtxt+"<font face='verdana' color='#FF0000'><b>"+"Good Day." + "</b></font><br><br>";

mailtxt=mailtxt+"<font face='verdana' color='#008080'><b>"+"May today be filled with sunshine and smile, laughter and love." + "</b></font><br><br>";

mailtxt=mailtxt+"<font face='verdana' color='#0000FF'><b>Cheers!" + "<br><br>";

mm.Body = mailtxt;

SmtpMail.SmtpServer="localhost";

SmtpMail.Send(mm);

}

}

}


10.Go to Design,right click and Select Add Installer ,you will notice two controls

     serviceProcessInstaller1,serviceInstaller1


11.Select  serviceProcessInstaller1 and go to its properties and change the Account

      type to 'Local System'


12. Select  serviceInstaller1 and go to its properties and change DisplayName and

      ServiceName to 'GoodDay' and make the StartType to 'Automatic'


13.Build the Solution(Cntrl+Shift+B)


14.Open the Visual Studio .Net Command Prompt and give the path of application

     for example C:\bin\Debug and then type InstallUtil GoodDay.exe
                

     to uninstall,  InstallUtil    /u GoodDay.exe         

 Sample screenshot

      


15. After Installation ,right click My Computer and

       Select 'Manage'

       and Select 'Service and Applications'

       and in that Select 'Services'

Select 'GoodDay' and start the Service.

                                     All the Best...................

Special thanks to Phani Kumar,Hyd.

                                                                             With Regards,

                                                                          Ravindra Donkada

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ravindradonkada
Web Developer
India India
Hi,
I am Ravindra, working as a .net programmer

You may also be interested in...

Comments and Discussions

 
QuestionWeb.Mail is not a Name Space to send the mail Pin
Aniket Sharma0226-Dec-13 7:45
memberAniket Sharma0226-Dec-13 7:45 
Questionget fail message when i am starting the service Pin
Member 950095418-Sep-13 6:27
memberMember 950095418-Sep-13 6:27 
AnswerRe: get fail message when i am starting the service Pin
Member 128759036-Dec-16 21:30
memberMember 128759036-Dec-16 21:30 
QuestionHow can turn this service to execute weekly. Pin
Sumaira Butt12-Sep-12 22:23
memberSumaira Butt12-Sep-12 22:23 
GeneralMy vote of 4 Pin
vsanju14-Mar-12 2:54
membervsanju14-Mar-12 2:54 
GeneralMy vote of 5 Pin
Sridhar Patnayak10-Jan-12 18:09
memberSridhar Patnayak10-Jan-12 18:09 
Questionone problem Pin
bhavinvaishnav117-Jul-11 22:15
memberbhavinvaishnav117-Jul-11 22:15 
QuestionRunning so nice.I need one more help Pin
praveena.karthi17-Jul-11 17:52
memberpraveena.karthi17-Jul-11 17:52 
Generalvery helpful Pin
atiaInam23-Jun-09 22:41
memberatiaInam23-Jun-09 22:41 
QuestionHow to Use this service in Web Application Pin
Malli.b3-Jan-08 1:25
memberMalli.b3-Jan-08 1:25 
Generalservice started mails send the next day onwords it is not sending mails Pin
HKum26-Oct-07 11:29
memberHKum26-Oct-07 11:29 
QuestionRunning this on the server Pin
EvoRich21-May-07 10:47
memberEvoRich21-May-07 10:47 
Questionnot working Pin
kgeethasen27-Dec-06 23:53
memberkgeethasen27-Dec-06 23:53 
hi ravindra

i am getting error while executing the program
"could not start the goodday on local computer. the service didnot return error. this could be a internal windows error"

can u give me an idea why it comes

thanking u ravindra

raju
Questionmail sending delayed till service stop Pin
seishin66619-Dec-06 4:19
memberseishin66619-Dec-06 4:19 
AnswerRe: mail sending delayed till service stop Pin
seishin66619-Dec-06 4:45
memberseishin66619-Dec-06 4:45 
QuestionRe: mail sending delayed till service stop Pin
seishin66619-Dec-06 22:25
memberseishin66619-Dec-06 22:25 
GeneralImages missing Pin
Balazs Zoltan11-Nov-06 5:29
memberBalazs Zoltan11-Nov-06 5:29 
AnswerRe: Images missing Pin
ravindradonkada17-Nov-06 21:39
memberravindradonkada17-Nov-06 21:39 
Questionsome problem Pin
accessred2-Jan-08 4:50
memberaccessred2-Jan-08 4:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 16 Nov 2006
Article Copyright 2006 by ravindradonkada
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid