public void Start() { _monik.ApplicationWarning("Started"); try { _nancyHost.Start(); } catch (Exception e) { _monik.ApplicationError(e.Message); } }
public void Send(string reportName, string[] addresses, string htmlReport = null, string jsonReport = null) { string filename = ""; bool hasHtml = !string.IsNullOrEmpty(htmlReport); bool hasJson = !string.IsNullOrEmpty(jsonReport); SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["SMTPServer"], 25); MailMessage msg = new MailMessage(); msg.From = new MailAddress(ConfigurationManager.AppSettings["from"]); foreach (var address in addresses) { msg.To.Add(new MailAddress(address)); } msg.Subject = reportName + $" {DateTime.Now:dd.MM.yy}"; if (hasHtml) { msg.IsBodyHtml = true; msg.Body = htmlReport; } if (hasJson) { Random rand = new Random(); filename = $@"{AppDomain.CurrentDomain.BaseDirectory}\\Report{rand.Next(32767)}.json"; using (FileStream fstr = new FileStream(filename, FileMode.Create)) { byte[] bytePage = System.Text.Encoding.UTF8.GetBytes(jsonReport); fstr.Write(bytePage, 0, bytePage.Length); } msg.Attachments.Add(new Attachment(filename)); } client.EnableSsl = true; client.DeliveryMethod = SmtpDeliveryMethod.Network; try { client.Send(msg); } catch (Exception ex) { _monik.ApplicationError($"Отчёт не выслан: " + ex.Message); } finally { msg.Dispose(); } if (hasJson) { File.Delete(filename); } }
private void CreateBase(string connStr) { try { _repository.CreateBase(connStr); } catch (Exception e) { _monik.ApplicationError(e.Message); } }
public void Execute(string address = null) { var dtoInstance = new DtoFullInstance() { StartTime = DateTime.Now, TaskId = Id, State = (int)InstanceState.InProcess }; dtoInstance.Id = _repository.CreateEntity(_mapper.Map <DtoInstance>(dtoInstance)); _repository.CreateEntity(_mapper.Map <DtoInstanceData>(dtoInstance)); string[] deliveryAddrs = { }; if (!string.IsNullOrEmpty(address)) { deliveryAddrs = new[] { address } } ; else if (SendAddresses != null) { deliveryAddrs = SendAddresses.GetAddresses(); } Stopwatch duration = new Stopwatch(); duration.Start(); int i = 1; bool dataObtained = false; string jsonReport = ""; string htmlReport = ""; while (!dataObtained && i <= TryCount) { try { jsonReport = _dataEx.Execute(this); htmlReport = _viewEx.ExecuteHtml(ViewTemplate, jsonReport); dataObtained = true; i++; break; } catch (Exception ex) { jsonReport = ex.Message; htmlReport = ex.Message; } i++; } if (dataObtained) { try { if (deliveryAddrs?.Length > 0) { _postMaster.Send(ReportName, deliveryAddrs, HasHtmlBody ? htmlReport : null, HasJsonAttachment ? jsonReport : null); } if (ChatId != 0) { try { _bot.SendTextMessageAsync(ChatId, _viewEx.ExecuteTelegramView(jsonReport, ReportName), ParseMode.Markdown).Wait(); } catch (Exception e) { Console.WriteLine(e); throw; } } _monik.ApplicationInfo($"Отчёт {Id} успешно выслан"); } catch (Exception e) { _monik.ApplicationError(e.Message); } } duration.Stop(); dtoInstance.Data = _archiver.CompressString(jsonReport); dtoInstance.ViewData = _archiver.CompressString(htmlReport); dtoInstance.TryNumber = i - 1; dtoInstance.Duration = Convert.ToInt32(duration.ElapsedMilliseconds); dtoInstance.State = dataObtained ? (int)InstanceState.Success : (int)InstanceState.Failed; // string filename = $@"{AppDomain.CurrentDomain.BaseDirectory}\\Report{Id}-{DateTime.Now:HHmmss}"; _repository.UpdateEntity(_mapper.Map <DtoInstance>(dtoInstance)); _repository.UpdateEntity(_mapper.Map <DtoInstanceData>(dtoInstance)); } //method