public static void SendMail() { ReadXlsFile(Configs.XlsFilePath, Configs.FiveDayMode, Configs.BirthdayColumnNumber, Configs.EmployeeNameColumnNumber); ReadHtmlFile(Configs.HtmlFilePath, Employees.CongratulationsList); if (Employees.WhosBirthdayIs.Count.Equals(0) || Configs.FiveDayMode && (DateTime.Now.DayOfWeek == DayOfWeek.Sunday || DateTime.Now.DayOfWeek == DayOfWeek.Saturday)) { Configs.AddLogsCollected($"Sending message: CANCELLED."); if (Configs.FiveDayMode && (DateTime.Now.DayOfWeek == DayOfWeek.Sunday || DateTime.Now.DayOfWeek == DayOfWeek.Saturday)) { Configs.AddLogsCollected($"Reason: today is a day off."); } if (Employees.WhosBirthdayIs.Count.Equals(0)) { Configs.AddLogsCollected($"Reason: employees don't have a birthday today."); } else if (Configs.FiveDayMode && (DateTime.Now.DayOfWeek == DayOfWeek.Sunday || DateTime.Now.DayOfWeek == DayOfWeek.Saturday)) { Configs.AddLogsCollected($"On Monday {Employees.WhosBirthdayIs.Count} employees will be congratulated."); } } else { if (Configs.EmailRecievers.Count.Equals(0)) { Configs.AddLogsCollected($"Sending message: CANCELLED."); Configs.AddLogsCollected($"Reason: recievers count equals 0."); } else { foreach (var reciever in Configs.EmailRecievers) { SendMessage(reciever, Configs.MessageSubject, Configs.MessageText); } } } SendLogs(); }
public static void EditConfig(string parameter, string value) { string fileType; switch (parameter) { case "birthdayColumnNumber": case "employeeNameColumnNumber": if (!Int32.TryParse(value, out _)) { value = ""; } break; case "serverPort": if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value)) { value = "25"; } else if (!Int32.TryParse(value, out _)) { value = ""; } break; case "htmlPath": fileType = value.Substring(value.LastIndexOf('.') + 1, value.Length - value.LastIndexOf('.') - 1); if (File.Exists(value) && fileType.ToLower().Equals("html")) { if (!File.ReadAllText(value).Contains("%LIST_OF_EMPLOYEES%")) { value = ""; } } else { value = ""; } break; case "xlsPath": fileType = value.Substring(value.LastIndexOf('.') + 1, value.Length - value.LastIndexOf('.') - 1); if (!File.Exists(value) || !fileType.ToLower().Equals("xls")) { value = ""; } break; case "senderPassword": value = EncryptString("b14ca5898a4e4133bbce2mbd02082020", value); break; case "fiveDaysMode": if (value.ToLower() == "yes" || value.ToLower() == "y") { value = "True"; } else { value = "False"; } break; default: break; } Configs.ChangeParameter(parameter, value); }
private static void ReadXlsFile(string path, bool fiveDaysMode, string birthdayColumn, string employeeColumn) { bool isNextMonthLoaded = false; bool isTempCopied = false; try { string fileName = path.Substring(path.LastIndexOf('\\')); File.Copy(path, Configs.TempPath + fileName, true); path = Configs.TempPath + fileName; isTempCopied = true; } catch { Configs.AddLogsCollected("Unable to open temporary copy of existing .xls file."); } try { int bdColumn = Convert.ToInt32(birthdayColumn); int emColumn = Convert.ToInt32(employeeColumn); bdColumn--; emColumn--; Workbook BirthdayBook = Workbook.Load(path); Worksheet BirthdaySheet = BirthdayBook.Worksheets[0]; for (int i = 0; i < BirthdaySheet.Cells.LastRowIndex; i++) { try { if (Convert.ToDateTime(BirthdaySheet.Cells[i, bdColumn].ToString()).Date > DateTime.Today) { if (!isNextMonthLoaded && Convert.ToDateTime(BirthdaySheet.Cells[i, bdColumn].ToString()).Date.Month.Equals(DateTime.Now.Month + 1)) { isNextMonthLoaded = true; break; } continue; } } catch { continue; } try { if (Convert.ToDateTime(BirthdaySheet.Cells[i, bdColumn].ToString()).Date.Equals(DateTime.Now.Date)) { Employees.AddBirthdaygiver(BirthdaySheet.Cells[i, emColumn].ToString()); } if (DateTime.Now.DayOfWeek == DayOfWeek.Monday && fiveDaysMode) { try { if (Convert.ToDateTime(BirthdaySheet.Cells[i, bdColumn].ToString()).Date.Equals(DateTime.Now.AddDays(-1).Date)) { Employees.AddBirthdaygiver(BirthdaySheet.Cells[i, emColumn].ToString()); } } catch { } try { if (Convert.ToDateTime(BirthdaySheet.Cells[i, bdColumn].ToString()).Date.Equals(DateTime.Now.AddDays(-2).Date)) { Employees.AddBirthdaygiver(BirthdaySheet.Cells[i, emColumn].ToString()); } } catch { } } } catch { continue; } try { } catch { continue; } } Configs.AddLogsCollected($"Reading xls: SUCCESS."); if (DateTime.Now.Day > 25 && !isNextMonthLoaded) { Configs.AddLogsCollected("Xls file does not contain list of employees for a next month."); } if (isTempCopied) { try { File.Delete(path); } catch { Configs.AddLogsCollected("Unable to delete temporary .xls file."); } } } catch { Configs.AddLogsCollected($"Reading xls: FAILURE."); } }
static void Main(string[] args) { Configs.AddLogsCollected($"\n\n\nCurrent user: {Environment.UserName}"); if (args.Length > 0) { for (int i = 0; i < args.Length; i++) { if (args[i].StartsWith("-")) { switch (args[i].ToLower()) { case "-silent": break; case "-help": Console.WriteLine($"\n-silent\t\t\t\tLaunch program without any GUI and output, excluding log.\n" + $"-showconfig\t\t\tShow current configuration stored in config.cfg file.\n" + $"-help\t\t\t\tDisplays help.\n" + $"-editconfig\t\t\tEdit current configuration stored in config.cfg file. " + $"\n\t\t\t\tUsage: -editconfig parameter=\"value\" \n" + $"\nList of parameters available:\n\n" + $"senderEmail\t\t\tE-mail address of sender.\n" + $"senderUsername\t\t\tE-mail server authorisation username.\n" + $"senderPassword\t\t\tE-mail server authorisation username. Note that you cannot change password via" + $"\n\t\t\t\tconfig.cfg manually due to encoding/decoding.\n" + $"senderName\t\t\tSender name displayed to reciever.\n\n" + $"emailRecievers\t\t\tList of e-mail recievers comma separated.\n" + $"messageSubject\t\t\tDisplayed subject of e-mail.\n" + $"htmlPath\t\t\tPath to html file. File should contain at least %LIST_OF_EMPLOYEES% string " + $"\n\t\t\t\tinside and has .html file extension.\n" + $"xlsPath\t\t\t\tPath to xls file. File should has .xls file extension.\n" + $"birthdayColumnNumber\t\tNumber of column, contains date of employee birthday. " + $"\n\t\t\t\tNote that date format starts with \' i.e \'{DateTime.Now}\n" + $"employeeNameColumnNumber\tNumber of column, contains employee full name.\n" + $"serverAddress\t\t\tIP-address of e-mail server.\n" + $"serverPort\t\t\tPort of e-mail server. If leaved empty - used default 25 port.\n" + $"fiveDaysMode\t\t\tWorking mode that allow send e-mails only from Monday to Friday. " + $"\n\t\t\t\tEmployees which birthday date happened on Saturday or Sunday " + $"\n\t\t\t\twill be congratulated on Monday.\n" + $"logRecievers\t\t\tList of logs recievers comma separated.\n" + $"\nUsage exaple:\n\n" + $"-editconfig [email protected] senderPassword=Qwerty123 htmlPath=C:\\temp\\file.html " + $"\nemailRecievers=\"[email protected], [email protected]\"\n"); break; case "-showconfig": foreach (var line in File.ReadAllLines(Configs.ConfigsPath)) { Console.WriteLine(line); } break; case "-editconfig": if (i + 1 <= args.Length) { try { Methods.LoadConfig(); } catch { Console.WriteLine("Unable to find previous configuration."); } for (int j = i + 1; j < args.Length && !args[j].StartsWith("-"); j++) { try { string parameter = args[j].Substring(0, args[j].IndexOf('=')); string value = args[j].Substring(args[j].IndexOf('=') + 1, args[j].Length - args[j].IndexOf('=') - 1); Methods.EditConfig(parameter, value); } catch { Console.WriteLine("Unable to edit configuration. Invalid parameter."); } i++; } } Methods.SaveConfig(); break; default: Console.WriteLine("Unknown parameter."); break; } } } if (args.Contains("-silent")) { try { Configs.AddLogsCollected("Working mode: silent"); Methods.LoadConfig(); Methods.SendMail(); } catch { Console.WriteLine("Unable to send message. Check configuration."); } } } else { //TODO: launch desktop app. } }