// // #endregion//Public Methods #region Private Methods // ***************************************************************** // **** Private Methods **** // ***************************************************************** // // // ************************************************************* // **** SendEmailAlert **** // ************************************************************* /// <summary> /// Method to send out email from the AlertManager thread. /// </summary> /// <param name="emailAddress"></param> /// <param name="alertRequest"></param> private void SendEmailAlert(string emailAddress, AlertManagerRequest alertRequest) { string subject = String.Empty; DateTime thisTime = Log.GetTime(); StringBuilder msg = new StringBuilder(); msg.AppendFormat("TimeStamp: {0} {1} \n", thisTime.ToShortDateString(), thisTime.ToString("HH:mm:ss.fff")); msg.Append(alertRequest.AlertString); // // Create email // System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage(); email.From = new System.Net.Mail.MailAddress(m_FromEmailAddress, "VioletAlert");//"*****@*****.**", "Tramp"); email.To.Add(emailAddress); email.Subject = string.Format("VioletAlert Level - {0}", alertRequest.Level); email.Body = msg.ToString(); // // Send message. // DateTime dt = Log.GetTime(); bool isSuccessful = true; System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(m_FromEmailSMTPServer, m_FromEmailSMTPPort); client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(m_FromEmailAddress, m_FromEmailPassword); client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; try { client.Send(email); } catch (Exception ex) { isSuccessful = false; Log.NewEntry(LogLevel.Error, "SendEmailNow: Exception={0}", ex.Message); } TimeSpan ts = Log.GetTime().Subtract(dt); Log.NewEntry(LogLevel.Minor, "SendEmailNow: Success = {0}. ElapsedTime = {1:0.000} ", isSuccessful.ToString(), ts.TotalSeconds); }
// // // // // // ************************************************************* // **** Hub Event Handler **** // ************************************************************* /// <summary> /// Main request handling routine processing all events originating from /// external and internal sources. /// Called only by the internal hub thread. /// </summary> /// <param name="eventArgList">Array of EventArgs to be processed.</param> protected override void HubEventHandler(EventArgs[] eventArgList) { foreach (EventArgs eventArg in eventArgList) // process each event { if (eventArg is AlertManagerRequest) { AlertManagerRequest alertRequest = (AlertManagerRequest)eventArg; switch (alertRequest.Type) { case AlertManagerRequest.Request.Stop: if (m_EmailQueue.IsEmpty) // we have no more emails to send out just shutdown { base.Stop(); } else { // we have more email to send out, send them and then shutdown UpdatePeriodic(); base.Stop(); } break; case AlertManagerRequest.Request.SendEmail: if (m_FromEmailAddress != string.Empty && m_FromEmailPassword != string.Empty) { m_EmailQueue.Enqueue(alertRequest); } else { Log.NewEntry(LogLevel.Error, "AlertManager: No send email adress has been set up. Ignoring email alert request."); } break; default: break; } } } }