Popular Posts

There was an error in this gadget

Dec 18, 2013

Send Auto Notification Using Windows service (asp.net)



-Open visual studio.
- Go to file->new->project
-Select windows service




















-right click on Service1.cs select properties
-In the properties window click on add Installer








-Right click on serviceInstaller1, select propertics
-Change DisplayName, ServiceName to SeraMailService
-StartType to Automatic

-Right click on serviceProcessInstaller1 then select Propertics
-change Account to LocalSystem
















-Right click on Service1.cs Design select ViewCode.
-In the OnStart method type the following code…


public void GetMail(object sender, System.Timers.ElapsedEventArgs args)
        {
            NetworkCredential cred = new NetworkCredential("it.system@lafarge.com", "Password");
            MailMessage msg = new MailMessage();
            msg.To.Add("jubayer@apsissolutions.com");
            msg.Subject = "Welcome JUBAYER";

            msg.Body = "You Have Successfully Entered to Sera's World!!!";
            msg.From = new MailAddress("jubayer@apsissolutions.com"); // Your Email Id
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            SmtpClient client1 = new SmtpClient("smtp.mail.yahoo.com", 465);
            client.Credentials = cred;
            client.EnableSsl = true;
            client.Send(msg);
        }

To keep the method running we add time interval

Add this line before onload method
System.Timers.Timer createOrderTimer;

In the onStart method type this code

createOrderTimer = new System.Timers.Timer();
            createOrderTimer.Elapsed += new System.Timers.ElapsedEventHandler(GetMail);
            createOrderTimer.Interval = 500;
            createOrderTimer.Enabled = true;
            createOrderTimer.AutoReset = true;
            createOrderTimer.Start();


Full code:

System.Timers.Timer createOrderTimer;

        public Service1()
        {
            InitializeComponent();
        }


        protected override void OnStart(string[] args)
        {
            createOrderTimer = new System.Timers.Timer();
            createOrderTimer.Elapsed += new System.Timers.ElapsedEventHandler(GetMail);
            createOrderTimer.Interval = 500;
            createOrderTimer.Enabled = true;
            createOrderTimer.AutoReset = true;
            createOrderTimer.Start();          
        }



        public void GetMail(object sender, System.Timers.ElapsedEventArgs args)
        {
            NetworkCredential cred = new NetworkCredential("it.system@lafarge.com", "Password");
            MailMessage msg = new MailMessage();
            msg.To.Add("jubayer@apsissolutions.com");
            msg.Subject = "Welcome JUBAYER";

            msg.Body = "You Have Successfully Entered to Sera's World!!!";
            msg.From = new MailAddress("jubayer@apsissolutions.com"); // Your Email Id
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            SmtpClient client1 = new SmtpClient("smtp.mail.yahoo.com", 465);
            client.Credentials = cred;
            client.EnableSsl = true;
            client.Send(msg);
        }


Now build the service using ctrl+shift+b


In the command mode type the code to install InstallUtil.exe

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe

To install the service use the command

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>InstallUtil.exe "C:\Documents and
Settings\Administrator\My Documents\Visual Studio 2008\Projects\SeraMailService\
SeraMailService\bin\Debug\SeraMailService.exe"


Type services.msc in the run windiw and enter to see all services
Right click on the service then run the service.


To uninstall the service use this code
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>InstallUtil.exe /u "C:\Documents and
Settings\Administrator\My Documents\Visual Studio 2008\Projects\SeraMailService\
SeraMailService\bin\Debug\SeraMailService.exe"