Avatar of spanout
spanout

asked on 

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
Microsoft Development

Avatar of undefined
Last Comment
spanout
Avatar of spanout
spanout

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of spanout
spanout

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft Development
Microsoft Development

Most development for the Microsoft platform is done utilizing the technologies supported by the.NET framework. Other development is done using Visual Basic for Applications (VBA) for programs like Access, Excel, Word and Outlook, with PowerShell for scripting, or with SQL for large databases.

48K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo