none
How do I make send email button send email to selected person in combobox and listbox? RRS feed

  • Question

  • Our app currently sends email to person(requestor) selected from a combobox. So it only sends one email to a single person.

    We have new business requirement that the app should also send email to more than one person when needed. The project owner does not want to replace the current combobox with a listbox. 

    So I was asked to add a listbox to the user interface. If a user want to send email to more than one person, the user will select a person from combobox and also select additional people to send email to from the listbox by clicking a Email button. 

    The code for Email button also need to check if the selected person in the listbox is also selected in the combobox. If the person selected in the listbox is also selected in the combobox, only one email will be sent. We do not want to send two emails to the same person.

    I need help to modify the code for the Email button so that, it also sends email or emails to a person or people selected from the listbox if and when there is a selection.

    How do I do this?

    The listbox is basically populated with the same information as combobox. 

    Here is listbox code

    public async void PopulateAdditionalStaffEmailListBox()
    {
        List<GetRequestorInfoModel> requestors = new List<GetRequestorInfoModel>();
        
        try
        {
            requestors = await FTACaseReset.Controllers.RequestorInfoController.GetAllRequestorInfoes();
            requestors = requestors.OrderBy(x => x.DisplayName).ToList(); //Has 15 items
    
            //Populate AdditionalStaffEmailListBox
            for (int i = 0; i < requestors.Count; i++)
            {
                ListBoxItem requestor = new ListBoxItem();
                requestor.Text = requestors[i].DisplayName;
                requestor.Value = requestors[i].RequestorInfoID;
                //AdditionalStaffEmailListBox.Items.Add(i);
                AdditionalStaffEmailListBox.Items.Add(requestor.Text).ToString();
            }
        }
        catch (Exception ex)
        {
            string errorMsg = string.Format("An error has occured in {0}. \nException:\n{1}", "AdditionalStaffEmailListBox()", ex.Message);
            MessageBox.Show(errorMsg, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

    Here is the code for email button that is sending emails 

    private async void SendEmail(int selectedBatch)
    {
        #region initialize progress bar
        EmailProgressBar.Visible = true;
        EmailProgressBar.Minimum = 0;
        EmailProgressBar.Maximum = 14;
        EmailProgressBar.Value = 1;
        EmailProgressBar.Step = 1;
        EmailButton.Visible = false;
        #endregion
        string message = "The following records have been prepped for processing.  Valid cases will be processed.{0}{1}{2}";
        string requestorName = string.Empty;
        string requestorEmail = string.Empty;
        //AdditionalStaffEmailListBox
        string AdditionalStaffRequestorEmail = string.Empty;
        string AdditionalStaffRequestorName = string.Empty;
    
        EmailProgressBar.PerformStep();
        List<GetCandidateCaseModel> masterCandidateCasesListToDisplay = new List<GetCandidateCaseModel>();
        EmailProgressBar.PerformStep();
    
        try
        {
            masterCandidateCasesListToDisplay = await Controllers.CandidateCaseController.GetAllCandidates();
            masterCandidateCasesListToDisplay = masterCandidateCasesListToDisplay.Where(x => x.BatchNumber == selectedBatch && x.RejectionReason != null).ToList();
            EmailProgressBar.PerformStep();
    
            if (masterCandidateCasesListToDisplay.Count > 0)
            {
                //code for AdditionalStaffEmailListBox
                AdditionalStaffRequestorName = masterCandidateCasesListToDisplay[0].RequestorInfo.DisplayName;
                AdditionalStaffRequestorEmail = masterCandidateCasesListToDisplay[0].RequestorInfo.Email;
                EmailProgressBar.PerformStep();
    
                requestorName = masterCandidateCasesListToDisplay[0].RequestorInfo.DisplayName;
                requestorEmail = masterCandidateCasesListToDisplay[0].RequestorInfo.Email;
                EmailProgressBar.PerformStep();
    
                string validCaseNumbers = "<hr /><br /><h3>VALID:</h3><table>";
                List<GetCandidateCaseModel> validCandidateCasesListToDisplay = await Controllers.CandidateCaseController.GetAllCandidates();
                validCandidateCasesListToDisplay = masterCandidateCasesListToDisplay.Where(x =>
                    x.RejectionReason.RejectionReasonDescription != Enumerations.Rejections.Discarded.ToString()
                    && x.RejectionReason.RejectionReasonDescription != Enumerations.Rejections.No_Records_To_Reset.ToString().Replace("_", " ")
                    && x.RejectionReason.RejectionReasonDescription != Enumerations.Rejections.Invalid_Case_Number.ToString().Replace("_", " ")
                    ).ToList();
                EmailProgressBar.PerformStep();
                foreach (GetCandidateCaseModel validCandidateCaseListToDisplay in validCandidateCasesListToDisplay)
                {
                    //Both penalties
                    if ((validCandidateCaseListToDisplay.FirstPenalty.Value) && (validCandidateCaseListToDisplay.SecondPenalty.Value))
                        validCaseNumbers += "<tr><td>" + validCandidateCaseListToDisplay.CaseNbr.ToString() + "</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>" + "Both Penalties" + "</td></tr>";
    
    
                    //If FirstPenalty = true display 1st Penalty
                    else if (validCandidateCaseListToDisplay.FirstPenalty.Value)
                        validCaseNumbers += "<tr><td>" + validCandidateCaseListToDisplay.CaseNbr.ToString() + "</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>" + "1st Penalty" + "</td></tr>";
    
                    //2nd Penalty
                    else if (validCandidateCaseListToDisplay.SecondPenalty.Value)
                        validCaseNumbers += "<tr><td>" + validCandidateCaseListToDisplay.CaseNbr.ToString() + "</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>" + "2nd Penalty" + "</td></tr>";
    
                }
                validCaseNumbers += "</table>";
                EmailProgressBar.PerformStep();
    
    
                string invalidCaseNumbers = "<hr /><br /><h3>INVALID:</h3><table>";
                List<GetCandidateCaseModel> invalidCandidateCasesListToDisplay = await Controllers.CandidateCaseController.GetAllCandidates();
                invalidCandidateCasesListToDisplay = masterCandidateCasesListToDisplay.Where(x =>
                    x.RejectionReason.RejectionReasonDescription != Enumerations.Rejections.Discarded.ToString()
                    && x.RejectionReason.RejectionReasonDescription != Enumerations.Rejections.Duplicate_Case_Number.ToString().Replace("_", " ")
                    && x.RejectionReason.RejectionReasonDescription != Enumerations.Rejections.Valid.ToString().Replace("_", " ")
                    ).ToList();
                EmailProgressBar.PerformStep();
    
                foreach (GetCandidateCaseModel invalidCandidateCaseListToDisplay in invalidCandidateCasesListToDisplay)
                {
                    invalidCaseNumbers += "<tr><td>" + invalidCandidateCaseListToDisplay.CaseNbr.ToString() + "</td><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td><td>" + invalidCandidateCaseListToDisplay.RejectionReason.RejectionReasonDescription + "</td></tr>";
                }
                invalidCaseNumbers += "</table>";
                EmailProgressBar.PerformStep();
    
                string discardedCaseNumbers = string.Empty;
                List<GetCandidateCaseModel> discardedCandidateCasesListToDisplay = await Controllers.CandidateCaseController.GetAllCandidates();
                discardedCandidateCasesListToDisplay = masterCandidateCasesListToDisplay.Where(x => x.RejectionReason.RejectionReasonDescription == Enumerations.Rejections.Discarded.ToString()).ToList();
                EmailProgressBar.PerformStep();
                if (discardedCandidateCasesListToDisplay.Count > 0)
                    discardedCaseNumbers = "<hr /><br /><h3>DISCARDED:</h3>" + string.Join("<br />", discardedCandidateCasesListToDisplay.Select(p => p.CaseNbr.ToString()));
    
                message = string.Format(message, validCaseNumbers, invalidCaseNumbers, discardedCaseNumbers);
                EmailProgressBar.PerformStep();
    
                using (MailMessage mailMessage = new MailMessage())
                {
                    mailMessage.From = new MailAddress("NoReply_FTA@courts.state.wt.us");
                    MailAddress to = new MailAddress(requestorEmail);
                    mailMessage.To.Add(to);
    
                    string ccEmailAddress = Authentication.GetADEmail();
                    if (ccEmailAddress.Length > 0)
                    {
                        MailAddress ccto = new MailAddress(ccEmailAddress);
                        mailMessage.CC.Add(ccto);
                    }
                    EmailProgressBar.PerformStep();
    
                    mailMessage.Subject = "FTA Case Reset Notice";
                    mailMessage.Body = message;
                    mailMessage.IsBodyHtml = true;
                    EmailProgressBar.PerformStep();
    
                    SmtpClient smtpClient = new SmtpClient();
                    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtpClient.PickupDirectoryLocation = @"c:\smtp";
                    smtpClient.EnableSsl = false;
                    smtpClient.UseDefaultCredentials = false;
                    smtpClient.Timeout = 60000;
                    EmailProgressBar.PerformStep();
    
                    smtpClient.Send(mailMessage);
                    MessageBox.Show("An email has been sent to " + requestorName, "Email", MessageBoxButtons.OK, MessageBoxIcon.Information);
    
                }
            }
            else
                MessageBox.Show("No Requestor was found.  Unable to send an email.", "Email", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    
            EmailProgressBar.PerformStep();
            EmailProgressBar.Visible = false;
            EmailProgressBar.Value = 0;
            EmailButton.Visible = true;
        }
        catch (Exception ex)
        {
            string errorMsg = string.Format("An error has occured in {0}. \nException:\n{1}", "SubmitButton_Click()", ex.Message);
            MessageBox.Show(errorMsg, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }


    Friday, June 28, 2019 3:58 PM