コード例 #1
0
 public static NotifyMailer getInstance()
 {
     if (instance == null)
     {
         instance = new NotifyMailer();
     }
     return(instance);
 }
コード例 #2
0
        public void Notify()
        {
            if (pullers.TrueForAll(x => x.Status == AsyncReplicaOperations.RunningStatusEnum.Completed))
            {
                if (Properties.Settings.Default.UseNotify)
                {
                    try
                    {
                        var notifier = NotifyMailer.getInstance();

                        var body = "<html><head><style type = 'text/css'>";
                        body += "h1{text-align:center;font-family:arial;font-size:22px;}";
                        body += "body{background-color:azure}";
                        body += "caption{margin-bottom:15px;}";
                        body += "table{margin-top:10px;border-spacing:0;border-collapse:collapse;font-family:sans-serif;width:100%}";
                        body += "td{border:1px solid black;font-size:13px;font-family:arial;}";
                        body += "th{background-color:darkgreen;color:lavender;font-size:14px;font-family:sans-serif;}";
                        body += "tr.evenrow{background-color:floralwhite;}";
                        body += "tr.notevenrow{background-color:lightgreen;}";
                        body += "</style></head>";
                        body += "<body><h1>Выгрузка реплик от " + DateTime.Now.ToString("dd.MM.yyyy") + "</h1>";

                        var listEnum = pullers.GetEnumerator();
                        while (listEnum.MoveNext())
                        {
                            var asyncPuller   = listEnum.Current;
                            var sqlConnection = new SqlConnection(asyncPuller.ConnectionString);
                            body += "<table>";
                            body += "<caption>Сервер " + sqlConnection.DataSource + " база данных " + sqlConnection.Database + "</caption>";
                            body += "<tr><th>Номер ОПС</th><th>Результат</th><th>Время обработки</th></tr>";

                            var logEnum   = asyncPuller.LogList.GetEnumerator();
                            var rowNumber = 0;
                            while (logEnum.MoveNext())
                            {
                                body += "<tr class='";
                                body += (rowNumber % 2 == 0) ? "evenrow" : "notevenrow";
                                body += "'>";
                                rowNumber++;
                                var rowLog = logEnum.Current;
                                body += "<td>" + rowLog.Id + "</td>";
                                body += "<td>" + rowLog.Log + "</td>";
                                body += "<td>" + rowLog.Time + "</td>";
                                body += "</tr>";
                            }


                            body += "</table>";
                        }

                        body += "</body></html>";

                        notifier.MessageBody = body;
                        var recepEnum = Properties.Settings.Default.MailRecep.Split(';').GetEnumerator();
                        var recepList = new List <string>();
                        while (recepEnum.MoveNext())
                        {
                            recepList.Add(recepEnum.Current.ToString());
                        }
                        var recepCCEnum = Properties.Settings.Default.MailCCRecep.Split(';').GetEnumerator();
                        var recepCCList = new List <string>();
                        while (recepCCEnum.MoveNext())
                        {
                            recepCCList.Add(recepCCEnum.Current.ToString());
                        }
                        notifier.sendMail(recepList, recepCCList);
                        Console.WriteLine("Сообщение успешно отправлено");
                    }
                    catch (Exception exception)
                    {
                        MessageBox.Show(string.Format("Оповещение невозможно отправить оповещение по причине следующей ошибки : {0}", exception.Message), "Возникла ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                Progress.IsIndeterminate = false;
                Progress.Value           = Progress.Maximum;
            }
        }