Question

I am using C#.NET in VS2010. In my below code I check each record in a datagrid for having been selected for Reporting. If selected, I then check each of the 3 available contact fields to see if that entity needs to be contacted by having the generated report emailed to them.

Filenames are saved as “IN_MUL_*BusinessID_Date_Time_ContactPoint*”, ex. “IN_MUL_25_05202013_09.11.14_BUS”. If I select 2 test results to report from the same Sample#, meaning they have the same ContactResults structure on their Account, 2 email dialogues open to the same person, with the same report attached. In this situation, the code is processing correctly, but logically I only need the one dialogue as it's the same report to the same individual.

Does anyone know of a fairly easy way I could check if a dialogue to a person and with a particular attachment has already been created, and if so, ignore that processing?

My code is below:

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

Any help would be greatly appreciated.

Was it helpful?

Solution

You could use a HashSet to remember the dialogs already processed. Store a concatenation of recipient, separator character (eg '~') and attachment as HashSet item. Use the Contains() method to find if the current combination has been there already.

By the way:

In your code you could use the following statement:

using Outlook = Microsoft.Office.Interop.Outlook;

This allows you to use Outlook rather than the lengthy Microsoft.Office.Interop.Outlook to shorten your code and clean it up.

The code could look like this:

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();
                    }
                }
            }
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top