예제 #1
0
 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());
 }
예제 #2
0
        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());
        }
예제 #3
0
        public bool RunReport(QlikViewReport report, SmtpServer smtpServer)
        {
            string outputFile;
            if (!Directory.Exists("ReportRunTest"))
                Directory.CreateDirectory("ReportRunTest");
            var error = this.ExportReport(report, "ReportRunTest", out outputFile);

            if (error.HasError)
                return false;

            return true;
        }
예제 #4
0
        public SmtpServerManager()
        {
            string folder = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Remove(System.Reflection.Assembly.GetExecutingAssembly().CodeBase.LastIndexOf("/"));

            folder     = folder.Replace("file:///", "");
            ConfigFile = folder + "/" + System.Configuration.ConfigurationManager.AppSettings["ReportConfig"];

            this._xmlDoc = new XmlDocument();
            this._xmlDoc.Load(ConfigFile);

            XmlNode smtpServerRootNode = this._xmlDoc.SelectSingleNode("ReportConfig/SmtpServer");

            //Smtp Server
            this.SmtpServer        = new SmtpServer();
            this.SmtpServer.Server = smtpServerRootNode.GetNodeValueIf("Server");
            this.SmtpServer.PickupDirectoryLocation = smtpServerRootNode.GetNodeValueIf("PickupDirectoryLocation");
            this.SmtpServer.User          = smtpServerRootNode.GetNodeValueIf("User");
            this.SmtpServer.Password      = EncryptionDecryption.Decode(smtpServerRootNode.GetNodeValueIf("Password"));
            this.SmtpServer.ExceptionFrom = smtpServerRootNode.GetNodeValueIf("ExceptionMailFrom");
            this.SmtpServer.ExceptionTo   = smtpServerRootNode.GetNodeValueIf("ExceptionMailTo");
            this.SmtpServer.UsePickupDirectoryLocation = bool.Parse(smtpServerRootNode.GetNodeValueIf("UsePickupDirectoryLocation"));
        }
예제 #5
0
        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;
        }
예제 #6
0
        private IError PopulateReportContext(ReportTask task, SmtpServer smtpServer, out List<ReportContext> exportedReports, out List<string> missingReports)
        {
            exportedReports = new List<ReportContext>();
            missingReports = new List<string>();

            List<ReportContext> excelReports = new List<ReportContext>();

            string folder = string.Empty;
            if (task.OutputFolder.EndsWith(@"\"))
            {
                folder = task.OutputFolder.Remove(task.OutputFolder.Length - 1);
            }
            else
            {
                folder = task.OutputFolder;
            }
            IError error = new QvError();

            #region Export report
            foreach (var item in task.Reports.Values)
            {
                this.Logger.Message("Running report [" + item.Name + "]");
                this.Logger.Message("Setting connection [" + item.Connection.Name + "]");

                string fileName;

                error = this.ExportReport(item, folder, out fileName);

                if (error.HasError == false)
                {
                    if (item.ReportType != Common.ReportType.Excel)
                        exportedReports.Add(new ReportContext()
                        {
                            Name = item.Name,
                            Description = item.Description,
                            OutputFullName = fileName,
                            ReportType = item.ReportType,
                            HtmlFormat = this.GetHtml(item, fileName)
                        });
                    else
                        excelReports.Add(new ReportContext()
                        {
                            Name = item.Name,
                            Description = item.Description,
                            OutputFullName = fileName,
                            ReportType = item.ReportType,
                            HtmlFormat = this.GetHtml(item, fileName)
                        });
                    this.Logger.Message("Export report [" + item.Name + "] complete");
                }
                else
                {
                    missingReports.Add(fileName);
                    this.Logger.Error(string.Format("The report [{0}] cannot be exported.", fileName));
                }
            }

            if (task.IsMergeInSingleExcel)
            {
                this.Logger.Message("Begin Merge in single excel");
                ReportContext mergedReport;
                string mergedFileName = folder + @"\" + task.Name + ".xls";

                error = this.MergeExcelReports(excelReports, mergedFileName, task.Name, out mergedReport);

                if (error.HasError == false)
                {
                    exportedReports.Add(mergedReport);
                    this.Logger.Message("Succed to merge in single excel");
                }
                else
                {
                    this.Logger.Message("Failed to merge, retain multiple files \n" + error.ErrorMessage);
                    MailHelper.ExceptionNotify("Failed to merge files for task " + task.Name, error.ErrorMessage.ToString(), smtpServer);
                }
            }
            else
            {
                exportedReports.AddRange(excelReports);
            }
            #endregion

            return error;
        }
예제 #7
0
 public SmtpClientAdaptor(SmtpServer smtpServer)
 {
     this._smtpServer = smtpServer;
 }
예제 #8
0
 public SmtpClientAdaptor(SmtpServer smtpServer)
 {
     this._smtpServer = smtpServer;
 }