[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 401
  • Last Modified:

Wrapper class cycles through all open emails when trying to send

I have use the enclosed wrapper class and have succesfully hooked the attachment and detachment events. When I hook up the send event, it behaves oddly.If I open 3 new emails, and send on any of them, the send handler fires for each open email. I can see this as the Guid on each rmail inspector can be seen when debugging. I need to keep the event handler in the wrapper so that I can read the Id which I use to retrive information from the AttachmentCount dictionary in ThisAddIn.cs. InspectorWrapper.cs Can anyone see why it would fire for each inspector, the attachments do not fire for each attachment.
using System;
using System.Collections.Generic;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;
using System.Collections;
namespace OutlookGuard
{
    public partial class ThisAddIn
    {

        public bool IsLicensed = true;
        public string Licensor = "";
        public string ExpireDate = "";



   
        /// <summary>
        /// Holds a reference to the Application.Inspectors collection
        /// Required to get notifications for NewInspector events.
        /// </summary>
        private Outlook.Inspectors _inspectors;

        /// <summary>
        /// A dictionary that holds a reference to the Inspectors handled by the add-in
        /// </summary>
        private Dictionary<Guid, InspectorWrapper> _wrappedInspectors;
        public Dictionary<Guid, int> AttachmentCount;

        
         

        /// <summary>
        /// Startup method is called when the add-in is loaded by Outlook
        /// </summary>
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {


            #region license check

            if (Properties.Settings.Default.Licence != "")
            {
                olicense myLicence = new olicense();
                oOutlookUtilities mysender = new oOutlookUtilities();

                myLicence.AccountEMail = Convert.ToString(mysender.getSenderAddress()[0]);
                myLicence.key = Properties.Settings.Default.Licence;
                myLicence.decodeLicense();
                if (myLicence.Error != "")
                {
                    MessageBox.Show("There was an error starting OutlookGuard: " + myLicence.Error);
                    IsLicensed = false;
                }
                else
                {
                    Licensor = myLicence.UserName;
                    if (myLicence.warningdays > 370) { ExpireDate = "Perpetual"; }
                    if (myLicence.warningdays < 370) { ExpireDate = Convert.ToString(myLicence.ExpiresDate).Split(' ')[0]; }

                }

                myLicence = null;
                mysender = null;
            }
            else
            {
                MessageBox.Show("This product is unregistered. Please go to the OutlookGuard website to obtain a license key, and enter the key in the settings area.");

            }

            #endregion


            _wrappedInspectors = new Dictionary<Guid, InspectorWrapper>();
            AttachmentCount = new Dictionary<Guid, int>();

            _inspectors = Globals.ThisAddIn.Application.Inspectors;
            _inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(WrapInspector);
            


            // Handle also already existing Inspectors
            // (e.g. Double clicking a .msg file)
            foreach (Outlook.Inspector inspector in _inspectors)
            {
                WrapInspector(inspector);
            }
        }

        /// <summary>
        /// Wraps an Inspector if required and remember it in memory to get events of the wrapped Inspector
        /// </summary>
        /// <param name="inspector">The Outlook Inspector instance</param>
        void WrapInspector(Outlook.Inspector inspector)
        {
            InspectorWrapper wrapper = InspectorWrapper.GetWrapperFor(inspector);
            if (wrapper != null)
            {
                // register for the closed event
                wrapper.Closed += new InspectorWrapperClosedEventHandler(wrapper_Closed);
                // remember the inspector in memory
                _wrappedInspectors[wrapper.Id] = wrapper;
                AttachmentCount[wrapper.Id] = 0;

            }
        }

        /// <summary>
        /// Method is called when an inspector has been closed
        /// Removes reference from memory
        /// </summary>
        /// <param name="id">The unique id of the closed inspector</param>
        void wrapper_Closed(Guid id)
        {
            _wrappedInspectors.Remove(id);
            AttachmentCount.Remove(id);
        }

        /// <summary>
        /// Shutdown method is called when Outlook is unloading the add-in
        /// </summary>
        /// 







        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            // do the homework and cleanup
            _wrappedInspectors.Clear();
            AttachmentCount.Clear();
            _inspectors.NewInspector -= new Outlook.InspectorsEvents_NewInspectorEventHandler(WrapInspector);
            _inspectors = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}

Open in new window

MailItemWrapper.cs
0
spanout
Asked:
spanout
  • 2
1 Solution
 
spanoutAuthor Commented:
What I want to use is this:

Item.Send += new Outlook.ApplicationEvents_11_ItemSendEventHandler(gp2_fnItemSend);

But send is a method not event, how can I do this?
0
 
spanoutAuthor Commented:
I have fixed it:

            ((Outlook.ItemEvents_10_Event)Item).Send += new Outlook.ItemEvents_10_SendEventHandler(gp2_fnItemSend);

POinting to
void gp2_fnItemSend(ref bool cancel)
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now