Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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

C#
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);
    }
}
Posted
Updated 28-Jun-19 6:04am
v4

1 solution

Quote:
The combobox actually have the same requestors (names with emails) as the listbox.

Pick ONE or the OTHER; not both.

Or is this about confusing the user?
 
Share this answer
 
Comments
Member 11403304 27-Jun-19 17:13pm    
This is not about confusing the user. This is the way I figured out to populate the listbox from the same database table. The project owner does not want to add extra work by removing the current combobox and replacing it with a listbox. So the solution I was given is to create a listbox and populate it with the same names from the database. This listbox is for sending email to extra people a sure you decide to send to.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900