Solved

window service in c#

Posted on 2014-10-10
8
191 Views
Last Modified: 2014-10-24
I have below codes working fine in window service. Can you show me how to use timer?
My goal is to send email daily at 1:00 am (PST).

Thanks


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Net.Mail;
using System.Threading;

namespace ConsoleApplication1
{
    class Program : ServiceBase
    {
        static void Main(string[] args)
        {
            ServiceBase.Run(new Program());
        }
        public Program()
        {
            this.ServiceName = "My Service";
        }

       protected override void OnStart(string[] args)
          {
            base.OnStart(args);
            //TODO: place your start code here
             
            #region main code
            sendSingleEmail("email", "email", "test", "test", true);
            #endregion



          }
        protected override void OnStop()
          {
            base.OnStop();
            //TODO: clean up any variables and stop any threads
          }

        #region method:send email
        public static void sendSingleEmail(string from, string to, string subject, string body, bool isHtml)
        {
            String smtpServer = "smtp.com";
            System.Net.Mail.MailMessage MyMessage = new System.Net.Mail.MailMessage();
            MyMessage.Subject = subject;
            MyMessage.Body = body;
            MyMessage.From = new MailAddress(from);
            MyMessage.To.Add(to);
            MyMessage.IsBodyHtml = isHtml;
            SmtpClient emailClient = new SmtpClient(smtpServer,25);
            emailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            emailClient.EnableSsl = false;
            emailClient.Send(MyMessage);            
        }
        #endregion
    }
     
}
0
Comment
Question by:ITsolutionWizard
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
You shouldn't use a service for this.  A service is something that runs constantly, looping at an interval to perform tasks.  There is no reason to keep your program in memory for that long if all your doing is sending an email once every day.

What I would recommend is a windows console application that when launched will send your email.  This is then scheduled via a scheduled task on a server/workstation.
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
I have applications like this that need to run at interval. I always build plain-old Windows Forms and I schedule them in the Windows Tasks Scheduler. This way, it is easy for me to run anytime, to debug, ...

Check http://emoreau.com/Entries/Articles/2008/11/Passing-arguments-to-an-application-startup.aspx
0
 

Expert Comment

by:CHARU T
Comment Utility
Its best written as a winforms program with Timer class and its tick() used to send the email with smtpclient. Registering as a service is needed when it actually is a product. It can be run as a background task as well thats starts at windows restart.
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
what I don't like about a Windows app with a Timer is if an error occurs, it is sometime not able to restart properly. If it is a scheduler that runs it, it always starts fresh. Also, a schedule task can run even if the computer is not logged on.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:CHARU T
Comment Utility
You can handle all the errors and what to do, if conditions. Also, whether the system is logged on or restarted, such things can also be tested by subscribing to windows events in C# code itself. You can log errors onto a text file, and proceed. Make sure there is no pop-up message boxes so its run in the background without user intervention.
0
 

Author Comment

by:ITsolutionWizard
Comment Utility
Need codes sample
0
 

Expert Comment

by:CHARU T
Comment Utility
If you need code sample for Timer (?), then there are plenty in the web. Add a timer and do your email fn in the tick() event. You can also write a console app, and use a timer in it, log msgs and errors to a text file so there are no pop-ups.

What is your q exactly here?
0
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
I would agree with Eric on this . . . if there's an error the code might exit and not be called again.  With a scheduled task you're relying on the windows scheduler to call the executable.

For a console application:
sendSingleEmail  becomes the main of the program.  Then schedule the task on the computer and you're done.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now