/// <summary> /// Get SMTP Server Information /// </summary> /// <returns></returns> static public GlobalVars.ResultSMTP GetSMTPInfo() { GlobalVars.SMTP smtp = new GlobalVars.SMTP(); GlobalVars.ResultSMTP resultSMTP = new GlobalVars.ResultSMTP() { ReturnCode = 0, Message = "", ReturnValue = smtp, RecordsCount = 0, HttpStatusCode = "" }; try { logger.Trace("Entering into GetSMTPInfo Method ..."); using (ScanningDBContext DB = new ScanningDBContext()) { var results = DB.Smtp.FirstOrDefault(); if (results != null) { resultSMTP.RecordsCount = 1; smtp.HostName = (results.HostName ?? "").Trim(); smtp.PortNumber = results.PortNumber; smtp.SenderEmailAddress = (results.SenderEmailAddress ?? "").Trim(); smtp.EnableSSLFlag = Convert.ToBoolean(results.EnableSslflag); smtp.SenderName = (results.SenderName ?? "").Trim(); smtp.UserName = (results.UserName ?? "").Trim(); smtp.Password = (results.Password ?? "").Trim(); } else { //There is no record in the database } } resultSMTP.ReturnValue = smtp; resultSMTP.Message = "GetSMTPInfo transaction completed successfully. Number of records found: " + resultSMTP.RecordsCount; logger.Debug(resultSMTP.Message); } catch (Exception e) { logger.Error("Error:" + e.Message + "\n" + "Exception: " + e.InnerException); resultSMTP.ReturnCode = -2; resultSMTP.Message = e.Message; var baseException = e.GetBaseException(); resultSMTP.Exception = baseException.ToString(); } logger.Trace("Leaving GetSMTPInfo Method ..."); return(resultSMTP); }
/// <summary> /// /// </summary> /// <returns></returns> static public GlobalVars.ResultGeneric UpdateSMTP(GlobalVars.SMTP smtp) { GlobalVars.ResultGeneric result = new GlobalVars.ResultGeneric() { ReturnCode = 0, Message = "", RecordsCount = 0, HttpStatusCode = "" }; try { logger.Trace("Entering into UpdateSMTP Method ..."); using (ScanningDBContext DB = new ScanningDBContext()) { // Customer Names must be unique in the Database. The Name could be change but it must be unique Smtp Matching_Result = DB.Smtp.FirstOrDefault(); Smtp record = new Smtp(); record.HostName = smtp.HostName; record.PortNumber = smtp.PortNumber; record.SenderEmailAddress = smtp.SenderEmailAddress; record.SenderName = smtp.SenderName; record.UserName = smtp.UserName; record.Password = smtp.Password; record.EnableSslflag = smtp.EnableSSLFlag.ToString(); if (Matching_Result == null) { // DB.Smtp.Add(record); DB.Smtp.Add(record); DB.SaveChanges(); result.Message = "There was not information associated to an SMTP Server, so new records was created successfully."; } else { // Means --> table has a record and it will be updated Matching_Result.HostName = smtp.HostName; Matching_Result.PortNumber = smtp.PortNumber; Matching_Result.SenderEmailAddress = smtp.SenderEmailAddress; Matching_Result.SenderName = smtp.SenderName; Matching_Result.UserName = smtp.UserName; Matching_Result.Password = smtp.Password; Matching_Result.SenderName = record.SenderName; DB.SaveChanges(); result.Message = "SMTP Inforation was updated successfully."; } } logger.Debug(result.Message); } catch (Exception e) { logger.Error("Error:" + e.Message + "\n" + "Exception: " + e.InnerException); result.ReturnCode = -2; result.Message = e.Message; var baseException = e.GetBaseException(); result.Exception = baseException.ToString(); } logger.Trace("Leaving UpdateSMTP Method ..."); return(result); }
/// <summary> /// Get Scanning Services Application Configuration File /// </summary> /// <returns></returns> static public SSSClientGlobalVars.ResultSSSConfig GetSSSConfig() { ScanningServicesDataObjects.SSSClientGlobalVars.ConfigSTR SSSConfig = new ScanningServicesDataObjects.SSSClientGlobalVars.ConfigSTR(); ScanningServicesDataObjects.SSSClientGlobalVars.ResultSSSConfig resultSSSConfig = new ScanningServicesDataObjects.SSSClientGlobalVars.ResultSSSConfig() { ReturnCode = 0, Message = "", ReturnValue = SSSConfig, RecordsCount = 0, HttpStatusCode = "" }; try { using (ScanningDBContext DB = new ScanningDBContext()) { //Get General Inforamtion, including Database information GlobalVars.GeneralSettings generalSettings = new GlobalVars.GeneralSettings(); var resultGeneralSettings = DB.GeneralSettings.FirstOrDefault(); if (resultGeneralSettings != null) { SSSConfig.Debug = Convert.ToBoolean(resultGeneralSettings.DebugFlag); SSSConfig.CaptureApplication = resultGeneralSettings.CpapplicationFilePath; SSSConfig.ImageViewer = resultGeneralSettings.ImageViewerFilePath; SSSClientGlobalVars.DatabaseSTR database = new SSSClientGlobalVars.DatabaseSTR(); database.Server = resultGeneralSettings.Dbserver; database.UserName = resultGeneralSettings.DbuserName; database.Password = resultGeneralSettings.Dbpassword; database.Provider = resultGeneralSettings.Dbprovider; database.RDBMS = resultGeneralSettings.Dbrdbms; database.DatabaseName = resultGeneralSettings.Dbname; // Add Database information to the SSSConfig Data Structure SSSConfig.Database = database; } else { //There is no record for SMTP information in the database } //Get SMTP Information GlobalVars.SMTP smtp = new GlobalVars.SMTP(); var resulSMTP = DB.Smtp.FirstOrDefault(); if (resulSMTP != null) { SSSClientGlobalVars.EmailSTR email = new SSSClientGlobalVars.EmailSTR(); email.Host = resulSMTP.HostName; email.Port = Convert.ToString(resulSMTP.PortNumber); email.SenderAddress = resulSMTP.SenderEmailAddress; email.EnableSSL = Convert.ToBoolean(resulSMTP.EnableSslflag); email.SenderName = resulSMTP.SenderName; email.UserName = resulSMTP.UserName; email.Password = resulSMTP.Password; // Add Email information to the SSSConfig Data Structure SSSConfig.Email = email; } else { //There is no record for SMTP information in the database } //Get Kodak Information List <GlobalVars.JobExtended> jobs = new List <GlobalVars.JobExtended>(); int count = 0; var resultJobs = SQLFunctionsJobs.GetJobs(); //DB.Jobs; if (resultJobs != null) { SSSClientGlobalVars.KodakSTR kodak = new SSSClientGlobalVars.KodakSTR(); kodak.JobTypes = new List <string>(); kodak.StationsID = new List <string>(); kodak.JobOutpts = new List <string>(); kodak.CaptureProScanDirectories = new List <string>(); foreach (var job in resultJobs.ReturnValue) { count++; kodak.JobTypes.Add(job.JobName); kodak.CaptureProScanDirectories.Add(job.ScanningFolder); kodak.JobOutpts.Add(job.PostValidationWatchFolder); kodak.StationsID.Add(Convert.ToString(count)); // Check if we are using Station ID for anything in SSS } // Add Kodak information to the SSSConfig Data Structure SSSConfig.Kodak = kodak; } else { //There is no record for Jobs in the database } //Get Customer information GlobalVars.ResultWorkingFolders resultWorkingFolders = new GlobalVars.ResultWorkingFolders(); List <GlobalVars.Customer> customers = new List <GlobalVars.Customer>(); var resultCustomers = DB.Customers; if (resultCustomers != null) { SSSConfig.Customers = new List <SSSClientGlobalVars.CustomerSTR>(); foreach (var cust in resultCustomers) { SSSClientGlobalVars.CustomerSTR customer = new SSSClientGlobalVars.CustomerSTR(); customer.Name = cust.CustomerName; // Get Jobs for a giben Customer ID var customerJobs = from j in DB.Jobs join p in DB.Projects on j.ProjectId equals p.ProjectId join c in DB.Customers on p.CustomerId equals c.CustomerId where c.CustomerId == cust.CustomerId select new { j, c.CustomerId, c.CustomerName, p.ProjectName }; if (resultJobs != null) { customer.Projects = new List <SSSClientGlobalVars.CustomerProjectSTR>(); foreach (var job in customerJobs) { resultWorkingFolders = SQLFunctionsGeneralSettings.GetWorkingFolderByID(job.j.RestingLocationId); if (resultWorkingFolders.RecordsCount > 0) { customer.BatchesLocation = resultWorkingFolders.ReturnValue[0].Path; } else { customer.BatchesLocation = ""; } // We will use projects as Jobs SSSClientGlobalVars.CustomerProjectSTR customerProject = new SSSClientGlobalVars.CustomerProjectSTR(); customerProject.Name = job.ProjectName; customerProject.ExportClass = job.j.ExportClassName; // Get Job Fields to deterimine which one are Keystrokes fields var jobFields = DB.JobsFields.Where(x => x.JobId == job.j.JobId); if (resultCustomers != null) { customerProject.KeyStrokesExcludedFields = new List <string>(); foreach (var field in jobFields) { if (field.KeyStrokeExcludeFlag.ToLower() == "true") { customerProject.KeyStrokesExcludedFields.Add(field.CpfieldName); } } } else { } // Get VFR Seetings customerProject.VFR = new SSSClientGlobalVars.VFRSTR(); var vfrSetting = DB.Vfr.FirstOrDefault(x => x.JobId == job.j.JobId); if (vfrSetting != null) { customerProject.VFR.CADIWebServiceURL = vfrSetting.Cadiurl; customerProject.VFR.UserName = vfrSetting.UserName; customerProject.VFR.Password = vfrSetting.Password; customerProject.VFR.InstanceName = vfrSetting.InstanceName; customerProject.VFR.RepositoryName = vfrSetting.RepositoryName; customerProject.VFR.QueryField = vfrSetting.QueryField; } else { } // Add Job information to the SSSConfig Data Structure customer.Projects.Add(customerProject); } } else { } // Get Customer Reports var reports = from r in DB.Reports join t in DB.ReportsTemplate on r.TemplateId equals t.TemplateId join c in DB.Customers on r.CustomerId equals c.CustomerId where c.CustomerName == customer.Name select new { r, t.Name }; if (reports.Count() >= 1) { customer.Reports = new List <SSSClientGlobalVars.ReportSTR>(); foreach (var record in reports) { SSSClientGlobalVars.ReportSTR report = new SSSClientGlobalVars.ReportSTR(); report.Name = record.Name; report.Recipients = new List <string>(); if (!string.IsNullOrEmpty(record.r.EmailRecipients)) { string[] recipients = record.r.EmailRecipients.Split(","); foreach (string recipient in recipients) { report.Recipients.Add(recipient); } } report.Subject = record.r.EmailSubject; report.Enable = Convert.ToBoolean(record.r.EnableFlag); SSSClientGlobalVars.ReportTitleSTR title1 = new SSSClientGlobalVars.ReportTitleSTR(); title1.Content = record.r.TitleContent1; title1.FontBold = record.r.TitleFontBoldFlag1; title1.FontColor = record.r.TitleFontColor1; title1.FontSize = Convert.ToString(record.r.TitleFontSize1); report.Title1 = title1; SSSClientGlobalVars.ReportTitleSTR title2 = new SSSClientGlobalVars.ReportTitleSTR(); title2.Content = record.r.TitleContent2; title2.FontBold = record.r.TitleFontBoldFlag2; title2.FontColor = record.r.TitleFontColor2; title2.FontSize = Convert.ToString(record.r.TitleFontSize2); report.Title2 = title2; SSSClientGlobalVars.ReportTitleSTR title3 = new SSSClientGlobalVars.ReportTitleSTR(); title3.Content = record.r.TitleContent3; title3.FontBold = record.r.TitleFontBoldFlag3; title3.FontColor = record.r.TitleFontColor3; title3.FontSize = Convert.ToString(record.r.TitleFontSize3); report.Title3 = title3; SSSClientGlobalVars.ReportTableSTR table = new SSSClientGlobalVars.ReportTableSTR(); table.HeaderBackGroundColor = record.r.TableHeaderBackColor; table.HeaderFontBold = record.r.TableHeaderFontBoldFlag; table.HeaderFontColor = record.r.TableHeaderFontColor; table.HeaderFontSize = Convert.ToString(record.r.TableHeaderFontSize.Value); //TableColumnNamesBackColor = record.r.TableColumnNamesBackColor, //TableColumnNamesFontBoldFlag = Convert.ToBoolean(record.r.TableColumnNamesFontBoldFlag), //TableColumnNamesFontColor = record.r.TableColumnNamesFontColor, //TableColumnNamesFontSize = record.r.TableColumnNamesFontSize.Value, report.ReportTable = table; var parameters = from rp in DB.ReportParameters join rtp in DB.ReportsTemplateParameters on rp.ParameterId equals rtp.ParameterId where (rp.ReportId == record.r.ReportId && rp.TemplateId == record.r.TemplateId) select new { rp, rtp.Name }; if (parameters.Count() >= 1) { report.Parameters = new List <SSSClientGlobalVars.ReportParameterSTR>(); foreach (var p in parameters) { SSSClientGlobalVars.ReportParameterSTR parameter = new SSSClientGlobalVars.ReportParameterSTR(); parameter.Name = p.Name; parameter.Value = p.rp.Value; report.Parameters.Add(parameter); } } customer.Reports.Add(report); } } // Add Customer information to the SSSConfig Data Structure SSSConfig.Customers.Add(customer); } } else { //There is no record for Jobs in the database } // Add System Reports to the SSSConfig Data Structure // Get Customer Reports var systemReports = from r in DB.Reports join t in DB.ReportsTemplate on r.TemplateId equals t.TemplateId where r.CustomerId == 0 select new { r, t.Name }; if (systemReports.Count() >= 1) { SSSConfig.Reports = new List <SSSClientGlobalVars.ReportSTR>(); foreach (var record in systemReports) { SSSClientGlobalVars.ReportSTR report = new SSSClientGlobalVars.ReportSTR(); report.Name = record.Name; report.Recipients = new List <string>(); if (!string.IsNullOrEmpty(record.r.EmailRecipients)) { string[] recipients = record.r.EmailRecipients.Split(","); foreach (string recipient in recipients) { report.Recipients.Add(recipient); } } report.Subject = record.r.EmailSubject; report.Enable = Convert.ToBoolean(record.r.EnableFlag); SSSClientGlobalVars.ReportTitleSTR title1 = new SSSClientGlobalVars.ReportTitleSTR(); title1.Content = record.r.TitleContent1; title1.FontBold = record.r.TitleFontBoldFlag1; title1.FontColor = record.r.TitleFontColor1; title1.FontSize = Convert.ToString(record.r.TitleFontSize1); report.Title1 = title1; SSSClientGlobalVars.ReportTitleSTR title2 = new SSSClientGlobalVars.ReportTitleSTR(); title2.Content = record.r.TitleContent2; title2.FontBold = record.r.TitleFontBoldFlag2; title2.FontColor = record.r.TitleFontColor2; title2.FontSize = Convert.ToString(record.r.TitleFontSize2); report.Title2 = title2; SSSClientGlobalVars.ReportTitleSTR title3 = new SSSClientGlobalVars.ReportTitleSTR(); title3.Content = record.r.TitleContent3; title3.FontBold = record.r.TitleFontBoldFlag3; title3.FontColor = record.r.TitleFontColor3; title3.FontSize = Convert.ToString(record.r.TitleFontSize3); report.Title3 = title3; SSSClientGlobalVars.ReportTableSTR table = new SSSClientGlobalVars.ReportTableSTR(); table.HeaderBackGroundColor = record.r.TableHeaderBackColor; table.HeaderFontBold = record.r.TableHeaderFontBoldFlag; table.HeaderFontColor = record.r.TableHeaderFontColor; table.HeaderFontSize = Convert.ToString(record.r.TableHeaderFontSize.Value); //TableColumnNamesBackColor = record.r.TableColumnNamesBackColor, //TableColumnNamesFontBoldFlag = Convert.ToBoolean(record.r.TableColumnNamesFontBoldFlag), //TableColumnNamesFontColor = record.r.TableColumnNamesFontColor, //TableColumnNamesFontSize = record.r.TableColumnNamesFontSize.Value, report.ReportTable = table; var parameters = from rp in DB.ReportParameters join rtp in DB.ReportsTemplateParameters on rp.ParameterId equals rtp.ParameterId where (rp.ReportId == record.r.ReportId && rp.TemplateId == record.r.TemplateId) select new { rp, rtp.Name }; if (parameters.Count() >= 1) { report.Parameters = new List <SSSClientGlobalVars.ReportParameterSTR>(); foreach (var p in parameters) { SSSClientGlobalVars.ReportParameterSTR parameter = new SSSClientGlobalVars.ReportParameterSTR(); parameter.Name = p.Name; parameter.Value = p.rp.Value; report.Parameters.Add(parameter); } } SSSConfig.Reports.Add(report); } } } } catch (Exception e) { logger.Error("Error:" + e.Message + "\n" + "Exception: " + e.InnerException); resultSSSConfig.ReturnCode = -2; resultSSSConfig.Message = e.Message; var baseException = e.GetBaseException(); resultSSSConfig.Exception = baseException.ToString(); return(resultSSSConfig); } logger.Trace("Leaving GetCustomerByID Method ..."); return(resultSSSConfig); }
/// <summary> /// Send Email /// </summary> /// <returns> Return 0 if the email was sent successfully</returns> static public GlobalVars.ResultGeneric SendEmail(GlobalVars.EMAIL email) { GlobalVars.ResultGeneric result = new GlobalVars.ResultGeneric() { ReturnCode = 0, Message = "", RecordsCount = 0, HttpStatusCode = "" }; try { logger.Trace("Entering into SendEmail Method ..."); // Get SMTP Server Information GlobalVars.ResultSMTP resultSMTP = new GlobalVars.ResultSMTP(); resultSMTP = SQLFunctionsSMTP.GetSMTPInfo(); GlobalVars.SMTP smpt = new GlobalVars.SMTP(); smpt = resultSMTP.ReturnValue; MailMessage mail = new MailMessage(); SmtpClient client = new SmtpClient(); mail.From = new MailAddress(smpt.SenderEmailAddress, smpt.SenderName); //mail.To.Add(new MailAddress(email.RecipientsEmailAddress)); foreach (var address in email.RecipientsEmailAddress.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries)) { mail.To.Add(address); } mail.Subject = email.Subject; mail.Body = email.Body; mail.IsBodyHtml = true; if (email.HasAttachment) { mail.Attachments.Add(new Attachment(new MemoryStream(email.attachment), "Report.pdf")); } client.Port = smpt.PortNumber; client.Host = smpt.HostName; //client.DeliveryMethod = SmtpDeliveryMethod.Network; if (resultSMTP.ReturnValue.EnableSSLFlag) { client.UseDefaultCredentials = false; client.EnableSsl = true; client.Credentials = new System.Net.NetworkCredential(smpt.UserName, smpt.Password); } else { client.EnableSsl = false; client.UseDefaultCredentials = true; } logger.Trace(" Sender: " + smpt.SenderName); logger.Trace(" Recipients: " + email.RecipientsEmailAddress); logger.Trace(" Subject: " + email.Subject); client.Send(mail); logger.Trace("Eamil was sent sucessfully."); } catch (Exception e) { logger.Error("Error:" + e.Message + "\n" + "Exception: " + e.InnerException); result.ReturnCode = -2; result.Message = e.Message; var baseException = e.GetBaseException(); result.Exception = baseException.ToString(); } logger.Trace("Leaving SendEmail Method ..."); return(result); }
public ActionResult UpdateSMTP([FromBody] string smtpJS) { var watch = System.Diagnostics.Stopwatch.StartNew(); GlobalVars.ResultGeneric result = new GlobalVars.ResultGeneric() { Message = "", ReturnCode = 0, //ReturnValue = "" }; try { if (smtpJS == null) { Response.StatusCode = (int)HttpStatusCode.BadRequest; result.ReturnCode = -1; result.Message = "Missing argument smtpJS"; logger.Warn("UpdateSMTP API Request ends with a Fatal Error."); logger.Warn(result.Message); } else { GlobalVars.SMTP smtp = JsonConvert.DeserializeObject <GlobalVars.SMTP>(smtpJS); logger.Info("UpdateSMTP API Request."); logger.Debug("Parameter:" + JsonConvert.SerializeObject(smtp, Formatting.Indented)); //Rules: // 1- HostName not null // 2- PortNumber <> 0 // 3- EnableSslFlag not null // 3- If SSL is true --> UserName and Password not null // 4- Sender Email Address not null // 5- Sender Name could be empty if (!string.IsNullOrEmpty(smtp.HostName) & smtp.PortNumber != 0 & !string.IsNullOrEmpty(smtp.SenderEmailAddress) & !string.IsNullOrEmpty(smtp.EnableSSLFlag.ToString())) { if (smtp.EnableSSLFlag) { if (!string.IsNullOrEmpty(smtp.UserName) & !string.IsNullOrEmpty(smtp.Password)) { // Good to go } else { Response.StatusCode = (int)HttpStatusCode.BadRequest; result.ReturnCode = -1; result.Message = "When SSL Flag is true, you must provide no empty values for UserName and Password"; logger.Warn("UpdateSMTP API Request ends with a Fatal Error."); logger.Warn(result.Message); } } else { // Good to go } } else { Response.StatusCode = (int)HttpStatusCode.BadRequest; result.ReturnCode = -1; result.Message = "HostName, PortNumber, and SenderEmailAddress must have a value. HostName not null, PortNumber > 0, SenderEmailAddress not null, and EnableSslflag {true, false}"; logger.Warn("UpdateSMTP API Request ends with a Fatal Error."); logger.Warn(result.Message); } if (result.ReturnCode == 0) { result = SQLFunctionsSMTP.UpdateSMTP(smtp); switch (result.ReturnCode) { case 0: logger.Info("UpdateSMTP API Request was executed Successfully."); Response.StatusCode = (int)HttpStatusCode.OK; break; case -2: Response.StatusCode = (int)HttpStatusCode.InternalServerError; logger.Fatal("UpdateSMTP API Request ends with a Fatal Error."); logger.Debug("Returned value:" + JsonConvert.SerializeObject(result, Formatting.Indented)); Response.StatusCode = (int)HttpStatusCode.InternalServerError; break; } } } } catch (Exception e) { logger.Fatal("UpdateSMTP API Request ends with a Fatal Error."); result.ReturnCode = -2; result.Message = e.Message; var baseException = e.GetBaseException(); result.Exception = baseException.ToString(); logger.Fatal("Returned value:" + JsonConvert.SerializeObject(result, Formatting.Indented)); Response.StatusCode = (int)HttpStatusCode.InternalServerError; } Response.ContentType = "application/json"; watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; elapsedMs = elapsedMs / 1000; result.ElapsedTime = elapsedMs.ToString(); result.HttpStatusCode = Response.StatusCode.ToString(); var messaje = JsonConvert.SerializeObject(result, Formatting.Indented); logger.Info("Leaving UpdateSMTP API"); //return Json(messaje); return(Content(messaje)); }