Пример #1
0
 private static void SendMessage(string reciever, string subject, string message)
 {
     try
     {
         MailAddress Sender   = new MailAddress(Configs.SenderEmail, Configs.SenderName);
         MailAddress Reciever = new MailAddress(reciever);
         MailMessage Message  = new MailMessage(Sender, Reciever)
         {
             Subject    = subject,
             Body       = message,
             IsBodyHtml = true
         };
         List <LinkedResource> images = new List <LinkedResource>();
         string[] htmlArray           = message.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
         string   htmlFolderPath      = Configs.HtmlFilePath.Substring(0, Configs.HtmlFilePath.LastIndexOf('\\') + 1);
         int      imageCounter        = 0;
         foreach (var line in htmlArray)
         {
             if (line.Contains("src="))
             {
                 string srcLine   = line.Substring(line.IndexOf('\"') + 1, line.Substring(line.IndexOf('\"') + 1).IndexOf('\"'));
                 string imagePath = htmlFolderPath + srcLine.Replace('/', '\\');
                 images.Add(new LinkedResource(imagePath, "image/gif"));
                 message = message.Replace(srcLine, "cid:" + images[imageCounter++].ContentId);
             }
         }
         var htmlView = AlternateView.CreateAlternateViewFromString(message, Encoding.UTF8, MediaTypeNames.Text.Html);
         images.ForEach(htmlView.LinkedResources.Add);
         Message.AlternateViews.Add(htmlView);  //I FORGOT SOMETHING!!!!
         SmtpClient Client = new SmtpClient(Configs.ServerAddress, Configs.ServerPort)
         {
             Credentials = new NetworkCredential(Configs.SenderUsername, DecryptString("b14ca5898a4e4133bbce2mbd02082020", Configs.SenderPassword)),
             EnableSsl   = false
         };
         Client.Send(Message);
         if (!subject.Contains("log from"))
         {
             Configs.AddLogsCollected($"Sending message to {reciever}: SUCCESS.");
             Configs.AddLogsCollected(LogConclusionMaker(reciever));
         }
     }
     catch
     {
         if (!subject.Contains("log from"))
         {
             Configs.AddLogsCollected($"Sending message to {reciever}: FAILURE.");
         }
         else
         {
             Configs.AddLogsCollected($"Sending log to {reciever}: FAILURE.");
         }
     }
 }
Пример #2
0
 private static void ReadHtmlFile(string path, string employees)
 {
     if (File.ReadAllText(path).Contains("%LIST_OF_EMPLOYEES%"))
     {
         Configs.MessageText = File.ReadAllText(path).Replace("%LIST_OF_EMPLOYEES%", employees);
         Configs.AddLogsCollected($"Reading html: SUCCESS.");
     }
     else
     {
         Configs.AddLogsCollected($"Reading html: FAILURE.");
         Configs.AddLogsCollected($"Reason: list of employees can't be inserted.");
     }
 }
Пример #3
0
 public static void SaveConfig()
 {
     try
     {
         File.WriteAllText(Configs.ConfigsPath, string.Empty);
         File.WriteAllLines(Configs.ConfigsPath, Configs.ParametersList);
         Configs.AddLogsCollected($"Config save: SUCCESS.");
     }
     catch
     {
         Configs.AddLogsCollected($"Config save: FAILURE.");
     }
 }
Пример #4
0
 public static void ChangeParameter(string parameter, string value)
 {
     if (parametersList.Contains(parametersList.FirstOrDefault(item => item.Contains(parameter))))
     {
         parametersList.Remove(parametersList.FirstOrDefault(item => item.Contains(parameter)));
         parametersList.Add(parameter + "=" + value);
         Configs.AddLogsCollected($"Config changed: " + parameter + "=" + value);
     }
     else
     {
         parametersList.Add(parameter + "=" + value);
         Configs.AddLogsCollected($"Config added: " + parameter + "=" + value);
     }
 }
Пример #5
0
 private static void SendLogs()
 {
     foreach (var reciever in Configs.LogsRecievers)
     {
         string ifSuccess = "";
         try
         {
             SendMessage(reciever, $"log from {DateTime.Now}", Configs.LogsCollected);
             ifSuccess = "SUCCESS";
         }
         catch
         {
             ifSuccess = "FAILURE";
         }
         finally
         {
             Configs.AddLogsCollected($"Sending logs: {ifSuccess}.");
         }
     }
 }
Пример #6
0
 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();
 }
Пример #7
0
        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.");
            }
        }
Пример #8
0
        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.
            }
        }