如何制作电子邮件按钮为选定的列表框项目发送电子邮件



我们有一个应用程序,该应用将电子邮件发送给从ComboBox中选择的人(请求者(。它只发送一封电子邮件。新要求是,我们希望向多个人发送多个电子邮件。项目所有者不想用listbox替换当前的ComboBox。这将需要其他数据库工作。因此,我建议解决方案的建议是添加一个与ComboBox相同的信息(数据库中的名称对象(填充的列表框。仅当用户想要向额外的人发送电子邮件时,才会使用列表框。我如何修改此按钮的代码,以便将电子邮件发送给ComboBox中选择的请求者(当前正在执行(,并将电子邮件发送给从列表框中选择的任何请求者?在发送电子邮件之前,我想检查以确保在列表框中也未选择ComboBox的所选请求者。我不希望请求者收到两封电子邮件。

这是列表框,它的请求者与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);
     }
}

这是当前正在将电子邮件发送给从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("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);
            }
        }

如果您不显示自定义类,请理解您的代码。以下代码应起作用,但请注意,比较显示名称不是最好的主意,因此,如果您可以通过某些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);
    }
}

相关内容

最新更新