adrianojorge 0 Newbie Poster

Good Morning to all :)

Currently i'm developing a Windows Service in order to automatically send me e-mails with my remote computer operating status.
I already checked MSDN documentation about this subject http://msdn.microsoft.com/en-us/library/ycy63t34.aspx and changed the code to use on my situation. But... i don't receive e-mails from it when the remote computer is normally turned off.

Someone could help me with this issue?

The code i'm using is:

using System;
    using System.ServiceProcess;
    using System.Threading;
    using System.Windows.Forms;
    using System.Diagnostics;
    using Microsoft.Win32;
    using System.ComponentModel;
    using System.Configuration.Install;
    using System.Net.Mail;
     
    namespace SimpleServiceCs
    {
        public class SimpleService : ServiceBase
        {
            static void Main(string[] args)
            {
                ServiceBase.Run(new SimpleService());
            }
     
            protected override void OnStart(string[] args)
            {
                EventLog.WriteEntry("SimpleService", "Starting SimpleService");
                new Thread(RunMessagePump).Start();
            }
     
            void RunMessagePump()
            {
                EventLog.WriteEntry("SimpleService.MessagePump", "Starting SimpleService Message Pump");
                Application.Run(new HiddenForm());
            }
     
            protected override void OnStop()
            {
                Application.Exit();
            }
        }
     
        public partial class HiddenForm : Form
        {
            public HiddenForm()
            {
                InitializeComponent();
            }
     
            private void HiddenForm_Load(object sender, EventArgs e)
            {
                SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_SessionEnding);
            }
     
            private void HiddenForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                SystemEvents.SessionEnding -= new SessionEndingEventHandler(SystemEvents_SessionEnding);
            }
    //****************************************************************************
    //          THIS IS THE PART OF CODE STRANGELY NOT WORKING!?!?
    //****************************************************************************
     
            private void SystemEvents_SessionEnding(object sender, EventArgs e)
            {
                EventLog.WriteEntry("System Logoff or System Shutdown");
                SendEMail("Your System is being Logged Off or Shutdown!");
            }
     
    //****************************************************************************
     
            private void SendEmail(string Warning){
     
            //Here goes all the code necessary for sending e-mails from my personal mail account
     
            }
     
     
        }
     
        partial class HiddenForm
        {
            private System.ComponentModel.IContainer components = null;
     
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
     
            private void InitializeComponent()
            {
                this.SuspendLayout();
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(0, 0);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                this.Name = "HiddenForm";
                this.Text = "HiddenForm";
                this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
                this.Load += new System.EventHandler(this.HiddenForm_Load);
                this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.HiddenForm_FormClosing);
                this.ResumeLayout(false);
     
            }
        }
     
        [RunInstaller(true)]
        public class SimpleInstaller : Installer
        {
            private ServiceInstaller serviceInstaller;
            private ServiceProcessInstaller processInstaller;
     
            public SimpleInstaller()
            {
                processInstaller = new ServiceProcessInstaller();
                serviceInstaller = new ServiceInstaller();
     
                // Service will run under system account
                processInstaller.Account = ServiceAccount.LocalSystem;
     
                // Service will have Start Type of Manual
                serviceInstaller.StartType = ServiceStartMode.Automatic;
     
                serviceInstaller.ServiceName = "Simple Service";
     
                Installers.Add(serviceInstaller);
                Installers.Add(processInstaller);
            }
        }
    }

Best Regards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.