public static void ExceptionNotify(string subject, string errorMessage, SmtpServer smtpServer) { Message message = new Message(); message.From = string.IsNullOrEmpty(smtpServer.ExceptionFrom) ? "*****@*****.**" : smtpServer.ExceptionFrom; message.Subject = subject; message.Body = errorMessage; SmtpClientAdaptor smtpClient = new SmtpClientAdaptor(smtpServer); smtpClient.SendEmail(message, smtpServer.ExceptionTo.Split(';').ToList()); }
public static void ExceptionNotify(string subject, string errorMessage, SmtpServer smtpServer) { Message message = new Message(); message.From = string.IsNullOrEmpty(smtpServer.ExceptionFrom) ? "*****@*****.**" : smtpServer.ExceptionFrom; message.Subject = subject; message.Body = errorMessage; SmtpClientAdaptor smtpClient = new SmtpClientAdaptor(smtpServer); smtpClient.SendEmail(message, smtpServer.ExceptionTo.Split(';').ToList()); }
public IError RunTask(ReportTask task, SmtpServer smtpServer) { IError taskError = new QvError(); try { this.Logger.Message("Begin to Run task [" + task.Name + "]------------------------------------------------"); this.Logger.Message("Preview the connection. "); this.ConnectionPreview(task.Reports.Values.First()); this.Logger.Message(string.Format("Task [{0}] have reports [{1}]", task.Name, string.Join(",", task.Reports.Keys.ToArray()))); List<ReportContext> reports; List<string> missingExportReports; IError error = this.PopulateReportContext(task, smtpServer, out reports, out missingExportReports); if (error.HasError) { taskError.HasError = true; taskError.ErrorMessage.AppendLine(error.ErrorMessage.ToString()); } else { #region Send Mail if (smtpServer.Validate() && reports.Count > 0) { SmtpClientAdaptor smtpClient = new SmtpClientAdaptor(smtpServer); //Get the recipients List<string> recipients = task.Recipients.Values.Select(x => x.Email).ToList(); if (task.Group != null) { foreach (var r in task.Group.RecipientList.Values) { if (!recipients.Contains(r.Email)) recipients.Add(r.Email); } } if (recipients.Count > 0) { if (task.IsSendMailInSingleMail) { smtpClient.SendEmail(task.MessageDefinition, reports, recipients); } else { foreach (var item in reports) { List<ReportContext> list = new List<ReportContext>(); list.Add(item); smtpClient.SendEmail(task.MessageDefinition, list, recipients); } } } else { this.Logger.Error("No recipients set up"); } } else { this.Logger.Error("SMTP SERVER Validate failed or there is no report exported, can not send email."); this.Logger.Info("Exported reports count " + reports.Count); } #endregion #region Upload to Ftp Server if (task.FtpServer != null) { string remoteFile = string.Empty; try { FtpClient client = new FtpClient(task.FtpServer.Host, task.FtpServer.Username, task.FtpServer.Password, task.FtpServer.Port); foreach (var item in reports) { if (!string.IsNullOrWhiteSpace(task.FtpServer.Folder)) remoteFile = task.FtpServer.Folder + (task.FtpServer.Folder.EndsWith("/") ? string.Empty : "/") + item.OutputFullName.Split("\\".ToArray())[item.OutputFullName.Split("\\".ToArray()).Length - 1]; else remoteFile = item.OutputFullName.Split("\\".ToArray())[item.OutputFullName.Split("\\".ToArray()).Length - 1]; client.Upload(remoteFile, item.OutputFullName); } } catch (Exception ex) { this.Logger.Error("Ftp upload failed!" + ex.Message + ex.StackTrace); MailHelper.ExceptionNotify("Ftp upload failed", ex.Message + ex.StackTrace + "\n" + "Remote file:" + remoteFile, smtpServer); } } #endregion if (missingExportReports.Count > 0) { string subject = "Some reports can not be exported"; string message = string.Join("\n\r", missingExportReports); MailHelper.ExceptionNotify(subject, message, smtpServer); taskError.HasError = true; taskError.ErrorMessage.AppendLine(message); } } } catch (Exception ex) { this.Logger.Error("Failed to run the task [" + task.Name + "] Error:" + ex.Message + "\n"+ ex.StackTrace); try { if (!string.IsNullOrEmpty(smtpServer.ExceptionTo)) { string subject = "QlikView Report Export&MailSend system Exception"; StringBuilder sb = new StringBuilder(); sb.AppendLine("Task name: " + task.Name); sb.AppendLine(string.Empty); sb.AppendLine("Exception message: " + ex.Message); sb.AppendLine("Stack Trace---------------"); sb.AppendLine(ex.StackTrace); MailHelper.ExceptionNotify(subject, sb.ToString(), smtpServer); taskError.HasError = true; taskError.ErrorMessage.AppendLine(sb.ToString()); } } catch (Exception ex1) { this.Logger.Error("Send exception mail failed. " + ex.Message + "\n" + ex1.StackTrace); } } this.Logger.Message("Complete running the task----------------------------------------------------------"); return taskError; }