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>();
    
    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);
    }
}


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

Preview



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