如何使“电子邮件”按钮为选定的列表框项目发送电子邮件?

时间:2019-06-27 21:04:34

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

这是当前向从组合框中选择的请求者发送电子邮件的按钮的代码

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

1 个答案:

答案 0 :(得分:1)

如果不显示自定义类,很难理解您的代码。 以下代码应该可以工作,但是请注意,比较显示名称不是最好的主意,因此,如果可以通过某些ID比较显示名称,则可以这样做。

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("NoReply_FTA@courts.state.mn.us");
            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);
    }
}