CsharpLearner2019
CsharpLearner2019

Reputation: 13

How do I make Email button sent email for selected listbox item?

We have an app that sends email to person(requestor) selected from a combobox. It only sends one email. New requirement is that we want to send more than one email to more than one person. The project owner does not want to replace the current combobox with a listbox. This will require additional database work. So what was suggested to me for a solution is to add a listbox which is populated with the same information (name objects from the database) as the combobox. The listbox will be used only when the user want to send email to extra people. How do I modify the code for this button so that it sends email to the requestor selected in the combobox (which it is currently doing) and also send email to any requestor selected from the listbox? Before the email is sent, I want to check to make sure that the selected requestor from the combobox is not also selected in the listbox. I do not want the requester to receive two email.

Here is the Listbox which has the same data for the requestor as the combobox.

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

Here is the code for the button that is currently sending email to requester selected 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;

                   using (MailMessage mailMessage = new MailMessage())
                    {
                        mailMessage.From = new MailAddress("[email protected]");
                        //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);
            }
        }

Upvotes: 0

Views: 117

Answers (1)

SinOfficial
SinOfficial

Reputation: 556

Its quite difficult understand your code if you dont show your custom classes. The following code should work but be aware that comparing display names is not the best idea so if you can compare them by some id, do that instead.

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();
        var selectedCandidate = masterCandidateCasesListToDisplay.Where(x => x.BatchNumber == selectedBatch && x.RejectionReason != null).ToList();

        if (masterCandidateCasesListToDisplay.Count > 0)
        {

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

            string requestorName0 = selectedCandidate[0].RequestorInfo.DisplayName;
            string requestorEmail0 = selectedCandidate[0].RequestorInfo.Email;

            MailMessage mailMessage = new MailMessage();
            MailAddress to = new MailAddress(requestorEmail);

            mailMessage.From = new MailAddress("[email protected]");
            mailMessage.To.Add(to);
            mailMessage.Subject = "FTA Case Reset Notice";
            mailMessage.Body = message;
            mailMessage.IsBodyHtml = true;

            string ccEmailAddress = Authentication.GetADEmail();
            if (ccEmailAddress.Length > 0)
            {
                MailAddress ccto = new MailAddress(ccEmailAddress);
                mailMessage.CC.Add(ccto);
            }

            foreach (ListViewItme item in AdditionalStaffEmailListBox.SelectedItems)
            {
                candidate = masterCandidateCasesListToDisplay.First(x => x.RequestorInfo.DisplayName == item.Value);

                requestorName = candidate.RequestorInfo.DisplayName;
                requestorEmail = candidate.RequestorInfo.Email;

                if (requestorEmail0 == requestorEmail)
                {
                    continue;
                }

                to = new MailAddress(requestorEmail);
                mailMessage.To.Add(to);

                ccEmailAddress = Authentication.GetADEmail();

                if (ccEmailAddress.Length > 0)
                {
                    MailAddress ccto = new MailAddress(ccEmailAddress);
                    mailMessage.CC.Add(ccto);
                }

                MessageBox.Show("An email has been sent to " + requestorName, "Email", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            smtpClient.Send(mailMessage);
        }
        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);
    }
}

Upvotes: 1

Related Questions