Ejemplo n.º 1
0
        private void sendEmailsButton_Click(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.emailContent == null)
            {
                LogLabel(logLabel, "Compose email first.", Color.Red);

                return;
            }

            if (employeeList.Count == 0)
            {
                LogLabel(logLabel, "Add employee to list first.", Color.Red);

                return;
            }

            if (passwordBox.TextLength == 0)
            {
                LogLabel(logLabel, "Enter password.", Color.Red);

                return;
            }

            List <PersonModel> selectedEmployees = new List <PersonModel>();

            if (!sendToTeamsCheckbox.Checked)
            {
                if (employeeListbox.SelectedItems.Count == 0)
                {
                    selectedEmployees = employeeList.ToList();
                }
                else
                {
                    foreach (PersonModel item in employeeListbox.SelectedItems)
                    {
                        selectedEmployees.Add(item);
                    }
                }
            }
            else
            {
                List <TeamModel> selectedTeams = new List <TeamModel>();

                if (teamListbox.SelectedItems.Count == 0)
                {
                    selectedTeams = teamList.ToList();
                }
                else
                {
                    foreach (TeamModel t in teamListbox.SelectedItems)
                    {
                        foreach (PersonModel p in t.TeamMembers)
                        {
                            selectedEmployees.Add(p);
                        }
                    }
                }
            }

            try
            {
                using (SmtpClient smtp = EmailLogic.CreateSmtp(Properties.Settings.Default.smtp,
                                                               int.Parse(Properties.Settings.Default.smtpPort), Properties.Settings.Default.email, passwordBox.Text))
                {
                    foreach (PersonModel p in selectedEmployees)
                    {
                        EmailLogic.logs.Add($"Sending Email to {p.FullName}");
                        logListbox.Refresh();
                        logListbox.TopIndex = logListbox.Items.Count - 1;

                        EmailLogic.logs.Add(EmailLogic.SendEmail(Properties.Settings.Default.emailContent, p, smtp, Properties.Settings.Default.email));
                        logListbox.Refresh();
                        logListbox.TopIndex = logListbox.Items.Count - 1;
                    }
                }
            }
            catch (Exception exc)
            {
                EmailLogic.logs.Add("Something went wrong. ");
                EmailLogic.logs.Add(exc.Message);

                logListbox.Refresh();
                logListbox.TopIndex = logListbox.Items.Count - 1;
            }

            passwordBox.Text = "";
        }