none
MailItem.To get cleared after checking entry ID? RRS feed

  • General discussion

  • Hi,

    I encountered a very strange problem when writing an Outlook 2007 plugin.

    My code is like below, using .Net 4.0 and Outlook 2007:

    //ThisAddIn.cs

    ...

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
      inspectors = Application.Inspectors;
    inspectors.NewInspector += newInspectorsEvents_NewInspectorEventHandler(inspectors_NewInspector);
    }
    void inspectors_NewInspector(Inspector Inspector)
    {
      handleComposeMail(Inspector);
    }
      
    privatevoid handleComposeMail(Inspector Inspector)
    {
      MailItem mailItem = Inspector.CurrentItem asMailItem;
      if (mailItem != null)
      {
        logger.Debug("Before checking entryID: " + mailItem.To); <--
        if (mailItem.EntryID == null)
        {
          logger.Debug("After checking entryID: " + mailItem.To);
          if (emailAddr != null && emailAddr.EndsWith(SpecialEmailSuffix))
          {
            // do actual stuff here
          }
        }
      }
    }

    And interestingly I got below log:

    2013-02-20 14:44:30.7355 DEBUG Before checking entryID: abcd@someemail.com
    2013-02-20 14:44:30.7525 DEBUG After checking entryID:

    So just to understand, why checking EntryID will clear MailItem.To?

    Wednesday, February 20, 2013 7:02 AM

All replies

  • if you remove check for entryid and just check twice mailitem.to - do you still get this strange behaviour?
    Wednesday, February 20, 2013 7:17 AM
  • if you remove check for entryid and just check twice mailitem.to - do you still get this strange behaviour?

    No.

    New code:

    logger.Debug("Before checking entryID: " + mailItem.To);
    logger.Debug("Before checking entryID 2: " + mailItem.To);
    if (mailItem.EntryID == null)
    {
      logger.Debug("After checking entryID: " + mailItem.To);
    }

    And I got:

    2013-02-20 15:36:16.7091 DEBUG Before checking entryID: abcd@someemail.com
    2013-02-20 15:36:16.7241 DEBUG Before checking entryID 2: abcd@someemail.com
    2013-02-20 15:36:16.7241 DEBUG After checking entryID:

    Wednesday, February 20, 2013 7:41 AM
  • if you add before checking entryid line like this:

    string html = maiItem.HTMLBody;

    does it change anything?

    if not, please move your code to Inspector.Activate event and check again

    Wednesday, February 20, 2013 9:35 AM
  • if you add before checking entryid line like this:

    string html = maiItem.HTMLBody;

    does it change anything?

    if not, please move your code to Inspector.Activate event and check again

    Tried adding the line before checking entryID, still same.

    Unfortunately I can't use Activate event as I need to capture compose new mail action.

    Anyway it's not a stopper for me I can always save the value to a local variable first. But just curious to know why reading entryID will clear To field here.

    Wednesday, February 20, 2013 10:20 AM
  • normallly you should always wait for activate event (which will fire just right after newinspector event) because only then all properties are safe to access. please try with activate event to see the outcome.

    Wednesday, February 20, 2013 11:26 AM
  • I've tried in below code:

    Since I need to get new Inspector reference, so I still listen to NewInspector event in ThisAddIn_Startup

    inspectors.NewInspector += new InspectorsEvents_NewInspectorEventHandler(inspectors_NewInspector);
    

    Then in the handler I listen to Activate event:

    void inspectors_NewInspector(Inspector Inspector)
    {
        ((InspectorEvents_10_Event)Inspector).Activate += new InspectorEvents_10_ActivateEventHandler(Inspector_Activate);
    }

    And in the handler:

    void Inspector_Activate()
    {
        MailItem mailItem = Application.ActiveInspector().CurrentItem as MailItem;
        logger.Debug("Deeply inside Inspector_Activate: " + mailItem.To);
    }

    But this Inspector_Activate() never get called. How do I listen to Activate event on new Inspector?

    Thanks.

    Thursday, February 21, 2013 3:24 AM
  • you have to store reference to object on which you listen for events, just like you store inspectors object. Put inspector in a class instance field along inspectors
    Thursday, February 21, 2013 4:55 AM