private static void SendEmailToAdmin(DataTable AErrors) { // Create excel output of the errors table string excelfile = TAppSettingsManager.GetValue("DataChecks.TempPath") + "/errors.xlsx"; try { using (StreamWriter sw = new StreamWriter(excelfile)) { using (MemoryStream m = new MemoryStream()) { if (!TCsv2Xml.DataTable2ExcelStream(AErrors, m)) { return; } m.WriteTo(sw.BaseStream); m.Close(); sw.Close(); } } } catch (Exception e) { TLogging.Log("Problems writing to file " + excelfile); TLogging.Log(e.ToString()); return; } if (TAppSettingsManager.HasValue("DataChecks.Email.Recipient")) { new TSmtpSender().SendEmail("<" + TAppSettingsManager.GetValue("DataChecks.Email.Sender") + ">", "OpenPetra DataCheck Robot", TAppSettingsManager.GetValue("DataChecks.Email.Recipient"), "Data Check", "there are " + AErrors.Rows.Count.ToString() + " errors. Please see attachment!", new string[] { excelfile }); } else { TLogging.Log("there is no email sent because DataChecks.Email.Recipient is not defined in the config file"); } }
private static void SendEmailForUser(TDataBase ADataBaseObj, string AUserId, DataTable AErrors) { TDBTransaction ReadTransaction = new TDBTransaction(); SUserRow userrow = null; // get the email address of the user ADataBaseObj.ReadTransaction(ref ReadTransaction, delegate { userrow = SUserAccess.LoadByPrimaryKey(AUserId, ReadTransaction)[0]; }); string excelfile = TAppSettingsManager.GetValue("DataChecks.TempPath") + "/errors" + AUserId + ".xlsx"; DateTime Errors_SinceDate = DateTime.Today.AddDays(-1 * SENDREPORTFORDAYS_TOUSERS); DataView v = new DataView(AErrors, "(CreatedBy='" + AUserId + "' AND ModifiedBy IS NULL AND DateCreated > #" + Errors_SinceDate.ToString("MM/dd/yyyy") + "#) " + "OR (ModifiedBy='" + AUserId + "' AND DateModified > #" + Errors_SinceDate.ToString("MM/dd/yyyy") + "#)", string.Empty, DataViewRowState.CurrentRows); try { using (StreamWriter sw = new StreamWriter(excelfile)) { using (MemoryStream m = new MemoryStream()) { if (!TCsv2Xml.DataTable2ExcelStream(v.ToTable(), m)) { return; } m.WriteTo(sw.BaseStream); m.Close(); sw.Close(); } } } catch (Exception e) { TLogging.Log("Problems writing to file " + excelfile); TLogging.Log(e.ToString()); return; } string recipientEmail = string.Empty; if (!userrow.IsEmailAddressNull()) { recipientEmail = userrow.EmailAddress; } else if (TAppSettingsManager.HasValue("DataChecks.Email.Recipient.UserDomain")) { recipientEmail = userrow.FirstName + "." + userrow.LastName + "@" + TAppSettingsManager.GetValue( "DataChecks.Email.Recipient.UserDomain"); } else if (TAppSettingsManager.HasValue("DataChecks.Email.Recipient")) { recipientEmail = TAppSettingsManager.GetValue("DataChecks.Email.Recipient"); } if (recipientEmail.Length > 0) { new TSmtpSender().SendEmail("<" + TAppSettingsManager.GetValue("DataChecks.Email.Sender") + ">", "OpenPetra DataCheck Robot", recipientEmail, "Data Check for " + AUserId, "there are " + v.Count.ToString() + " errors. Please see attachment!", new string[] { excelfile }); } else { TLogging.Log("no email can be sent to " + AUserId); } }