Last post Jul 30, 2009 04:11 PM by VegaVilas
Jul 30, 2009 04:11 PM|VegaVilas|LINK
Want to Help New People even i searched almost 3-4 months for this kind of Post for a full description long back but i got some time now to Post it.
Sending an Email Automatically for every 30 Minutes.
1.Open Microsoft Visual Studio 2008 Goto File New Project-Select Visual C# from the left pane,than underneath it Select Windows.From the Right Pane select Windows Service.
2.Give a Name to the Service as MyAutoService and Click Ok.
3.Service1.cs [Design] page is opened.Right Click on this page->Select Properties->Change the Name and ServiceName (first and Last of Properties Window) as MyAutoService.Verify that the AutoLog(second in the properties) is True(Most of the Time it is True).
4.Right Click on the same [Design] page once again and Select Add Installer which adds like two labels serviceProcessInstaller1 and serviceInstaller1.
5.Select serviceProcessInstaller1 (looks like a label/first one) and Right Click on that to go to its Properties Select Account(second in the property Window) ,Change From User to LocalSystem.
6.Select the other label serviceInstaller1 ,Right Click and Go to Properties Change the Display Name(3rd one in the properties window) as MyAutoService(This can be different from the one i gave at the time of New Project,This can be nice name which is used
to start and stop the service and make it automatic).
7.Change the StartType(Last One in Properties Window) from Manual to Automatic.You can also add a description to the service while you start the service or when someone tries to stop the service they know what it is related to and who is going to affect.
Description is the second one in the Properties window.
8.Go to Service1.cs[Design] which can be shown above as a Menu option.Right Click on it and select View Code ,the code window is opened.
9.code Window Looks Like:
10.Here before ServiceBase add a namespace System.ServiceProcess
for example:Now You code Looks Like:
//Here add some declaration
//for Example Add your connection string,if you use database connection
//And also here you can declare the variables you may use in sending the email,like from,to etc.,
private string con="Data Source=ServerName;User ID=user;Password=pwd;Initial Catalog=DatabaseName";
//here add a new bunch of code
using(SqLConnection sqlconnection=new Sqlconnection(connectionstring)
sqlcommand cm=new sqlcommand("your database query to fetch the email addresses",sqlconnection);
read all the records and store in the variables declared at the starting;
MailMessage mm=new MailMessage();
mm.Subject="My Email Subject";
mm.From=new MailAddress(From String) //variable if it is a database value or else mm.From=new MailAddress("Your email Id");
mm.Bcc.Add(new MailAddress("Email Address To whom you want to send an email");
mm.Body="Body of the email";
//If above Body is in Html format
smtp.Port =Port No.;
11.Go To Service1.Designer.cs and just Comment below line as we already declared in Service1.cs above the MyAutoService Constructor.
12.Save All and Goto Build and select Build MuAutoService.
Displays if there are any errors.Or else it says Build Suceeded.
13.Goro Start->All Programs->Microsoft Visual Studio 2008->Visual Studio command prompt.
By default it gives the path of Visual Studio environment.You know where your project is saved when you gave name to your project ,if it is the same just type cd/MyAutoservice\bin\debug
type cd\ which clears the path now type the path where you saved the project for example
this gived you the whole path upto debug
gives the messages that it is being Installed successfully.
14.Now Go to MyComputer->right Click and select Manage->Go to Services and Applications if you don't find an option called Services,you can find Services in Services and Applications select services.
15.Right hand side you can find a name called MyAutoService with a side description(what ever you placed).select it and right click than click start.This will start sending after 30 minutes and until you stop the service.
16.If you would like to stop the service.Right click on that and select Stop.
17.If you would like to make any change to this service.Goto command prompt and UnInstall before you make any change by setting your path of the project and typing as
InstallUtil /u MyAutoService.exe
which gives some messages saying UnInstall is successful.
18. Go to your Project Service,make changes,Build MyAutoService,Install and Start as in step 13 and 14.
Let me know if any one has any questions.
Iam successful with this Project.
Sorry If My Post caused any Trouble to AnyOne.