if a Winform is running, and sudden power shutdown occurs, would it able to send alert, such as email alert. I attached some sample code which is able to detect the system log-off and send email alert, but I would like it to be able to detect sudden power shutdown. Any constructive suggestions are welcomed. thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net.Mime;
namespace PowerShutDown
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static int WM_QUERYENDSESSION = 0x11;
private static bool systemShutdown = false;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_QUERYENDSESSION)
{
// MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot");
systemShutdown = true;
Email_Notify();
}
// If this is WM_QUERYENDSESSION, the closing event should be
// raised in the base WndProc.
base.WndProc(ref m);
} //WndProc
public void Email_Notify()
{
string Body = "Power ShutDown at " + DateTime.Now;
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("tanthiamhuat@gmail.com");
mail.To.Add("tanthiamhuat@pmail.ntu.edu.sg");
mail.To.Add("tanthiamhuat@yahoo.com");
mail.Subject = "Power Shutdown";
mail.Body = Body;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("tanthiamhuat", "12345678");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
// MessageBox.Show("Email is Sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}

1 answers
if you have no power to run a software you can't do a thing, but normally an nms server should be implemented in ur network that monitors the needed server so whenever a shutdown happen the server send an email or sms or anything u want. if u don't want to have an nms server and u have another pc just make a software that pings or checks the pc through the network if its on or not.
answered 7 months ago by:
1351