Click here to Skip to main content
14,453,651 members

How modify send email email button to allow selected listbox emails to be sent?

Member 11403304 asked:

Open original thread
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.

What I have tried:

Code for Listbox
public async void PopulateAdditionalStaffEmailListBox()
    List<GetRequestorInfoModel> requestors = new List<GetRequestorInfoModel>();
        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;
    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);

Code for email button

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;
    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;
    string AdditionalStaffRequestorEmail = string.Empty;
    string AdditionalStaffRequestorName = string.Empty;

    List<GetCandidateCaseModel> masterCandidateCasesListToDisplay = new List<GetCandidateCaseModel>();

        masterCandidateCasesListToDisplay = await Controllers.CandidateCaseController.GetAllCandidates();
        masterCandidateCasesListToDisplay = masterCandidateCasesListToDisplay.Where(x => x.BatchNumber == selectedBatch && x.RejectionReason != null).ToList();

        if (masterCandidateCasesListToDisplay.Count > 0)
            //code for AdditionalStaffEmailListBox
            AdditionalStaffRequestorName = masterCandidateCasesListToDisplay[0].RequestorInfo.DisplayName;
            AdditionalStaffRequestorEmail = masterCandidateCasesListToDisplay[0].RequestorInfo.Email;

            requestorName = masterCandidateCasesListToDisplay[0].RequestorInfo.DisplayName;
            requestorEmail = masterCandidateCasesListToDisplay[0].RequestorInfo.Email;

            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("_", " ")
            foreach (GetCandidateCaseModel validCandidateCaseListToDisplay in validCandidateCasesListToDisplay)
                //Both penalties
                if ((validCandidateCaseListToDisplay.FirstPenalty.Value) && (validCandidateCaseListToDisplay.SecondPenalty.Value))
                    validCaseNumbers += "<tr><td>" + validCandidateCaseListToDisplay.CaseNbr.ToString() + "</td><td>      </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>      </td><td>" + "1st Penalty" + "</td></tr>";

                //2nd Penalty
                else if (validCandidateCaseListToDisplay.SecondPenalty.Value)
                    validCaseNumbers += "<tr><td>" + validCandidateCaseListToDisplay.CaseNbr.ToString() + "</td><td>      </td><td>" + "2nd Penalty" + "</td></tr>";

            validCaseNumbers += "</table>";

            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("_", " ")

            foreach (GetCandidateCaseModel invalidCandidateCaseListToDisplay in invalidCandidateCasesListToDisplay)
                invalidCaseNumbers += "<tr><td>" + invalidCandidateCaseListToDisplay.CaseNbr.ToString() + "</td><td>      </td><td>" + invalidCandidateCaseListToDisplay.RejectionReason.RejectionReasonDescription + "</td></tr>";
            invalidCaseNumbers += "</table>";

            string discardedCaseNumbers = string.Empty;
            List<GetCandidateCaseModel> discardedCandidateCasesListToDisplay = await Controllers.CandidateCaseController.GetAllCandidates();
            discardedCandidateCasesListToDisplay = masterCandidateCasesListToDisplay.Where(x => x.RejectionReason.RejectionReasonDescription == Enumerations.Rejections.Discarded.ToString()).ToList();
            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);

            using (MailMessage mailMessage = new MailMessage())
                mailMessage.From = new MailAddress("");
                MailAddress to = new MailAddress(requestorEmail);

                string ccEmailAddress = Authentication.GetADEmail();
                if (ccEmailAddress.Length > 0)
                    MailAddress ccto = new MailAddress(ccEmailAddress);

                mailMessage.Subject = "FTA Case Reset Notice";
                mailMessage.Body = message;
                mailMessage.IsBodyHtml = true;

                SmtpClient smtpClient = new SmtpClient();
                smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtpClient.PickupDirectoryLocation = @"c:\smtp";
                smtpClient.EnableSsl = false;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Timeout = 60000;

                MessageBox.Show("An email has been sent to " + requestorName, "Email", MessageBoxButtons.OK, MessageBoxIcon.Information);

            MessageBox.Show("No Requestor was found.  Unable to send an email.", "Email", MessageBoxButtons.OK, MessageBoxIcon.Warning);

        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);
Tags: C#


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100