Wrapper class cycles through all open emails when trying to send

Posted on 2011-10-02
Last Modified: 2013-11-10
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;
                if (myLicence.Error != "")
                    MessageBox.Show("There was an error starting OutlookGuard: " + myLicence.Error);
                    IsLicensed = false;
                    Licensor = myLicence.UserName;
                    if (myLicence.warningdays > 370) { ExpireDate = "Perpetual"; }
                    if (myLicence.warningdays < 370) { ExpireDate = Convert.ToString(myLicence.ExpiresDate).Split(' ')[0]; }


                myLicence = null;
                mysender = null;
                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.");



            _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)

        /// <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)

        /// <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
            _inspectors.NewInspector -= new Outlook.InspectorsEvents_NewInspectorEventHandler(WrapInspector);
            _inspectors = null;

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

Open in new window

Question by:spanout

    Author Comment

    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?

    Accepted Solution

    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)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
    For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now