簡體   English   中英

如何使“電子郵件”按鈕為選定的列表框項目發送電子郵件?

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

我們有一個應用程序,可以向從組合框中選擇的人(請求者)發送電子郵件。 它只發送一封電子郵件。 新要求是我們要向多個人發送多個電子郵件。 項目所有者不想用列表框替換當前的組合框。 這將需要其他數據庫工作。 因此,為我建議的解決方案是添加一個列表框,該列表框填充有與組合框相同的信息(數據庫中的名稱對象)。 僅當用戶要將電子郵件發送給其他人時,才會使用該列表框。 如何修改此按鈕的代碼,以便將電子郵件發送到組合框中選擇的請求者(當前正在執行),並將電子郵件發送到從列表框中選擇的任何請求者? 在發送電子郵件之前,我要檢查以確保在列表框中也沒有選擇從組合框選擇的請求者。 我不希望請求者收到兩封電子郵件。

這是具有與組合框相同的請求者數據的列表框。

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

如果不顯示您的自定義類,則很難理解您的代碼。 以下代碼應該可以工作,但是請注意,比較顯示名稱不是最好的主意,因此,如果可以通過某些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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM