0

我在 VS2010 中使用 C#.NET。在下面的代码中,我检查数据网格中的每条记录是否已被选中进行报告。如果选中,然后我会检查 3 个可用联系人字段中的每一个,以查看是否需要通过将生成的报告通过电子邮件发送给他们来联系该实体。

文件名保存为“IN_MUL_*BusinessID_Date_Time_ContactPoint*”,例如。“IN_MUL_25_05202013_09.11.14_BUS”。如果我从同一个 Sample# 中选择 2 个测试结果来报告,这意味着它们在其帐户上具有相同的 ContactResults 结构,2 个电子邮件对话向同一个人打开,并附有相同的报告。在这种情况下,代码正在正确处理,但从逻辑上讲,我只需要一个对话,因为它是给同一个人的同一份报告。

有谁知道一种相当简单的方法,我可以检查是否已经创建了与某人的对话并具有特定的附件,如果是,请忽略该处理?

我的代码如下:

private void emailReports()
        {
            foreach (DataGridViewRow recRow in dgvTests.Rows)
            {
                if (((recRow.Cells["Select"].Value != null) && Boolean.Parse(recRow.Cells["Select"].Value.ToString())))
                {
                    // If Business should be contacted with Sample Test Results
                    if ((recRow.Cells["BusinessContactResults"].Value != null) && (Boolean.Parse(recRow.Cells["BusinessContactResults"].Value.ToString()) == true) && (recRow.Cells["BusinessEmail"].Value != null))
                    {
                        Microsoft.Office.Interop.Outlook.Application objApp = new Microsoft.Office.Interop.Outlook.Application();
                        Microsoft.Office.Interop.Outlook.MailItem mail = null;
                        mail = (Microsoft.Office.Interop.Outlook.MailItem)objApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
                        mail.To = recRow.Cells["BusinessEmail"].Value.ToString();
                        mail.Subject = "Inspection Notification";
                        mail.Body = "";
                        mail.Attachments.Add((object)string.Format("{0}IN_{1}_{2}_{3}_BUS.pdf", reptLoc, strLSN, recRow.Cells["BusinessID"].Value, dtString), Microsoft.Office.Interop.Outlook.OlAttachmentType.olEmbeddeditem, 1, (object)"Attachment");
                        mail.Display();
                    }
                    if ((recRow.Cells["SupplierContactResults"].Value != null) && (Boolean.Parse(recRow.Cells["SupplierContactResults"].Value.ToString()) == true) && (recRow.Cells["SupplierEmail"].Value != null))
                    {
                        Microsoft.Office.Interop.Outlook.Application objApp = new Microsoft.Office.Interop.Outlook.Application();
                        Microsoft.Office.Interop.Outlook.MailItem mail = null;
                        mail = (Microsoft.Office.Interop.Outlook.MailItem)objApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
                        mail.To = recRow.Cells["SupplierEmail"].Value.ToString();
                        mail.Subject = "Inspection Notification";
                        mail.Body = "";
                        mail.Attachments.Add((object)string.Format("{0}IN_{1}_{2}_{3}_SUP.pdf", reptLoc, strLSN, recRow.Cells["BusinessID"].Value, dtString), Microsoft.Office.Interop.Outlook.OlAttachmentType.olEmbeddeditem, 1, (object)"Attachment");
                        mail.Display();
                    }
                    if ((recRow.Cells["CorporateContactResults"].Value != null) && (Boolean.Parse(recRow.Cells["CorporateContactResults"].Value.ToString()) == true) && (recRow.Cells["CorporateEmail"].Value != null))
                    {
                        Microsoft.Office.Interop.Outlook.Application objApp = new Microsoft.Office.Interop.Outlook.Application();
                        Microsoft.Office.Interop.Outlook.MailItem mail = null;
                        mail = (Microsoft.Office.Interop.Outlook.MailItem)objApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
                        mail.To = recRow.Cells["CorporateEmail"].Value.ToString();
                        mail.Subject = "Inspection Notification";
                        mail.Body = "";
                        mail.Attachments.Add((object)string.Format("{0}IN_{1}_{2}_{3}_CRP.pdf", reptLoc, strLSN, recRow.Cells["BusinessID"].Value, dtString), Microsoft.Office.Interop.Outlook.OlAttachmentType.olEmbeddeditem, 1, (object)"Attachment");
                        mail.Display();
                    }
                }
            }
        }

任何帮助将不胜感激。

4

1 回答 1

1

您可以使用HashSet来记住已处理的对话框。将收件人、分隔符(例如“~”)和附件的串联存储为HashSet项目。使用该Contains()方法查找当前组合是否已经存在。

顺便一提:

在您的代码中,您可以使用以下语句:

using Outlook = Microsoft.Office.Interop.Outlook;

这允许您使用Outlook而不是冗长Microsoft.Office.Interop.Outlook来缩短您的代码并对其进行清理。

代码可能如下所示:

using Outlook = Microsoft.Office.Interop.Outlook;
using System.Collections.Generic;

namespace nsXYZ
{
    class AClass
    {
        private void emailReports()
        {
            HashSet<String> diaSet = new HashSet<String>();
            Outlook.Application objApp = new Outlook.Application();
            Outlook.MailItem mail = null;
            String s;

            foreach (DataGridViewRow recRow in dgvTests.Rows)
            {
                if (((recRow.Cells["Select"].Value != null) && Boolean.Parse(recRow.Cells["Select"].Value.ToString())))
                {
                    mail = null;

                    // If Business should be contacted with Sample Test Results
                    if ((recRow.Cells["BusinessContactResults"].Value != null) && (Boolean.Parse(recRow.Cells["BusinessContactResults"].Value.ToString()) == true) && (recRow.Cells["BusinessEmail"].Value != null))
                    {
                        mail = (Outlook.MailItem)objApp.CreateItem(Outlook.OlItemType.olMailItem);
                        mail.To = recRow.Cells["BusinessEmail"].Value.ToString();
                        mail.Attachments.Add((object)string.Format("{0}IN_{1}_{2}_{3}_BUS.pdf", reptLoc, strLSN, recRow.Cells["BusinessID"].Value, dtString), Outlook.OlAttachmentType.olEmbeddeditem, 1, (object)"Attachment");
                    }
                    if ((recRow.Cells["SupplierContactResults"].Value != null) && (Boolean.Parse(recRow.Cells["SupplierContactResults"].Value.ToString()) == true) && (recRow.Cells["SupplierEmail"].Value != null))
                    {
                        mail = (Outlook.MailItem)objApp.CreateItem(Outlook.OlItemType.olMailItem);
                        mail.To = recRow.Cells["SupplierEmail"].Value.ToString();
                        mail.Attachments.Add((object)string.Format("{0}IN_{1}_{2}_{3}_SUP.pdf", reptLoc, strLSN, recRow.Cells["BusinessID"].Value, dtString), Outlook.OlAttachmentType.olEmbeddeditem, 1, (object)"Attachment");
                    }
                    if ((recRow.Cells["CorporateContactResults"].Value != null) && (Boolean.Parse(recRow.Cells["CorporateContactResults"].Value.ToString()) == true) && (recRow.Cells["CorporateEmail"].Value != null))
                    {
                        mail = (Outlook.MailItem)objApp.CreateItem(Outlook.OlItemType.olMailItem);
                        mail.To = recRow.Cells["CorporateEmail"].Value.ToString();
                        mail.Attachments.Add((object)string.Format("{0}IN_{1}_{2}_{3}_CRP.pdf", reptLoc, strLSN, recRow.Cells["BusinessID"].Value, dtString), Outlook.OlAttachmentType.olEmbeddeditem, 1, (object)"Attachment");
                    }

                    if ((mail != null) && !diaSet.Contains(s = mail.To + "~" + mail.Attachments[0]))
                    {
                        diaSet.Add(s);
                        mail.Subject = "Inspection Notification";
                        mail.Body = "";
                        mail.Display();
                    }
                }
            }
        }
    }
于 2013-05-20T20:58:29.593 回答