Windows Service Scheduler.cs Code Problem

Last Reply on Apr 06, 2016 03:45 AM By Mahesh1986

Posted on Apr 06, 2016 01:52 AM

Hi ,

   This is my Scheduler.cs Code 

 

  private Timer timer1 = null;
        public Scheduler()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            timer1 = new Timer();
            this.timer1.Interval = 30000;
            this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
            this.timer1.Enabled = true;
            Library.WriteErrorLog("Service Has Been Started");
        }

        private void timer1_Tick(object sender, ElapsedEventArgs e)
        {
            Library.WriteErrorLog("Timer ticked and some job doing");
            sendEmailNotification();
            Library.WriteErrorLog("Timer ticked and some job Completed");

        }
        private bool sendEmailNotification()
        {
            bool isSend = false;
            try
            {
                MailMessage message = new MailMessage();
                message.To.Add("xxx@gmail.com");
                //            if (!string.IsNullOrEmpty(dEmail.CC))
                message.CC.Add("xxxx@gmail.com");
                //if (!string.IsNullOrEmpty(dEmail.BCC))
                //    message.Bcc.Add(dEmail.BCC);
                message.Subject = "Tezting";
                message.Body = "Testing";
                message.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Send(message);
                isSend = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            return isSend;
        }
        protected override void OnStop()
        {
            timer1.Enabled = false;
            Library.WriteErrorLog("Teh window service has been stopped");

        }

Problem : In my log file its always printing like beloow:

 

 

4/6/2016 12:41:23 PMService Has Been Started
4/6/2016 12:41:53 PMTimer ticked and some job doing
4/6/2016 12:42:23 PMTimer ticked and some job doing
4/6/2016 12:42:53 PMTimer ticked and some job doing
4/6/2016 12:43:23 PMTimer ticked and some job doing
4/6/2016 12:43:53 PMTimer ticked and some job doing
4/6/2016 12:44:23 PMTimer ticked and some job doing
4/6/2016 12:44:53 PMTimer ticked and some job doing
4/6/2016 12:45:23 PMTimer ticked and some job doing
4/6/2016 12:45:53 PMTimer ticked and some job doing

its always printing job is doing...but not completed message 

Posted on Apr 06, 2016 03:45 AM

Hi ,

With the following code Modification i acheived the functionlity

 

 MailMessage message = new MailMessage();
                message.To.Add("yyy@gmail.com");
                MailAddress from = new MailAddress("xxx", "Mahesh");
                message.From = from;
                message.Subject = "Enquiry From Mahesh";
                message.Body = "Hi";
                message.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
                credentials.UserName = "xxx@gmail.com";
                credentials.Password = "xxxxxx";
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = credentials;
                smtp.Port = 587;
                smtp.Send(messag

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html