예제 #1
0
        private void actionOnPowerChange(string color)
        {
            // Changes the color of the icon depending on what the battery state is.
            // Also sends the email, and records the data into a log.

            DateTime today    = DateTime.Now;
            string   mailBody = Environment.MachineName
                                + " is reporting a battery state change at "
                                + today.ToString("M/d/yyyy h:mm:ss tt");
            string mailSubjectBat  = Environment.MachineName + " On Battery Power";
            string mailSubjectMain = Environment.MachineName + " On Main Power";

            if (color == "red")
            {
                this.Icon        = Properties.Resources.iconRed;
                notifyIcon1.Icon = Properties.Resources.iconRed;
                lbl_status.Text  = "Battery Power";
                Task.Run(new Action(() => mail.sendNow(mailSubjectBat, mailBody)));

                /*
                 * try
                 * {
                 *  //Task.Run(new Action(() => mail.sendNow(mailSubjectBat,mailBody)));
                 *  //mail.sendNow(mailSubjectBat, mailBody);
                 * }
                 * catch (Exception ex)
                 * {
                 *  MessageBox.Show("Error sending email.\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 * }*/
                logging.writeLog(mailSubjectBat);
            }
            else
            {
                this.Icon        = Properties.Resources.icon;
                notifyIcon1.Icon = Properties.Resources.icon;
                lbl_status.Text  = "Main Power";
                Task.Run(new Action(() => mail.sendNow(mailSubjectMain, mailBody)));

                /*
                 * try
                 * {
                 *  Task.Run(new Action(() => mail.sendNow(mailSubjectMain, mailBody)));
                 *  //mail.sendNow(mailSubjectMain, mailBody);
                 * }
                 * catch (Exception ex)
                 * {
                 *  MessageBox.Show("Error sending email.\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 * }
                 */
                logging.writeLog(mailSubjectMain);
            }
        }
        private void testEmail()
        {
            this.Invoke(new Action <bool>(progressIsMoving), true);
            sendMail mail = new sendMail();

            try
            {
                mail.sendNow("Battery Notification Email Test", "Test email from Battery Notification");
                logging.writeLog("Sent test email");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error sending email.\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                logging.writeLog("Error sending email.\n" + ex.Message);
            }
            this.Invoke(new Action <bool>(progressIsMoving), false);
        }