none
How do I modify send email button to allow selected listbox emails to be sent? RRS feed

  • Question

  • We have an application that sends email to one person selected from a combobox.

    New requirement is to also allow a user to select more users from a listbox if and when a user want to send emails to more than one person. i.e. additional emails.

    There is a button, when clicked, it is currently sending email to only one person also known as requestor selected from a combobox.
    Application manager want me to add a listbox which I have and populate it with the same data from the database as the combobox.

    Now the challenge I am having is how to get the button to also send the email to the selected person (requestor) from the listbox.

    How do I modify the code below for the Email button so that it continues to send email to requestor selected from combobox as it is now doing and additionally also send email or emails to any selected requestors from the listbox?

    Also I would also need to check that the requestor in the listbox is not already selected from the combobox. I do not want to send email to the requestor selected in the combobox twice.

    If the requestor is already selected in the combobox, that user should not be selected in the listbox. Even if the user selects the same person in the listbox, only one email should be sent to that requester. However all other requestors selected in the listbox and have bot been selected in the combobox should be sent an email.

    Code for listbox populated with requestors i.e. These are objects. Each object contain name and email for each requestor

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

    The Email button code that is currently sending email to selected requestor from the combobox.

    private async void SendEmail(int selectedBatch)
    {
      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;
    
    List<GetCandidateCaseModel> masterCandidateCasesListToDisplay = new List<GetCandidateCaseModel>();
    try
    {
     masterCandidateCasesListToDisplay = await Controllers.CandidateCaseController.GetAllCandidates();
    masterCandidateCasesListToDisplay = masterCandidateCasesListToDisplay.Where(x => x.BatchNumber == selectedBatch && x.RejectionReason != null).ToList();
    if (masterCandidateCasesListToDisplay.Count > 0)
    {
     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("_", " ")).ToList();
    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>";
    
     using (MailMessage mailMessage = new MailMessage())
    {
      mailMessage.From = new MailAddress("NoReply_FTA@courts.state.mn.us");
    //Uncomment after testing June 2019 
     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);
     }
      mailMessage.Subject = "FTA Case Reset Notice";
      mailMessage.Body = message;
      mailMessage.IsBodyHtml = true;
      SmtpClient smtpClient = new SmtpClient();
      smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
      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);
    }
     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);
     }
    }





    • Edited by winkimjr2 Thursday, June 27, 2019 9:29 PM
    Thursday, June 27, 2019 7:31 PM