如何检查是否没有为同一电子邮件/文件名创建多个Outlook对话框?



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

文件名保存为"IN_MUL_*BusinessID_Date_Time_ContactPoint*",例如"IN_MUL_25_05202013_09.11.14_BUS"。如果我从相同的样本#中选择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();
                    }
                }
            }
        }

可以使用HashSet来记住已经处理过的对话框。存储收件人,分隔符(例如'~')和附件的连接作为HashSet项。使用Contains()方法查找当前组合是否已经存在。

By the way:

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

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

最新更新