1
votes

I wrote a simple VSTO addin for Outlook 2016(part of office 365) to check if there is some email addresses outside our company are mixed in the recipients list. I simplified the code like below:

      int countExternalAddress;
      string externalAddresses;
      string internalDomain=“@example.com”; 
     //indicates the email domain of our company, we use exchange server.

     private void Application_ItemSend(object Item, ref bool Cancel)
     {

        countExternalAddress = 0;
        externalAddresses="";

        Outlook.MailItem item = (Outlook.MailItem)Item;

        foreach (Outlook.Recipient recp in item.Recipients)
        {
             ConvertExchangeAddrToSMTPAddr(recp.AddressEntry.Address);
             //by access ExchangeUser.PrimarySmtpAddress
             CheckTheAddress(recp.AddressEntry.Address);
        }

        if (countExternalAddress > 0)
        {
           Warn();
        }

     }

The code works with no problem most of time, but some times Warn() function shows warnings based on (part of) the recipients of the LAST email, not the CURRENT one. The problem cannot be reproduced all the time, but when it occurs, the procedure is like:

  1. Send an email to internal recipients(my colleagues, exchange addresses), the exchange addresses are successfully converted to SMTP address, because they are predefined as "internal" address, the mail will be sent without warning.
  2. send another email to some other internal recipients, the program shows warning of "external address mixed", and the "external" address are from the previously sent email(mentioned in 1 above, not all addresses just one of them), and, the detected external address is in Exchange address format which is supposed to be converted to SMTP format. Strangely, I cannot find the address in the recipients list of the current email. If I save the current email and restart outlook, when I pick the saved email and try to resend it, no such "external" address will be detected again.

    It looks like the Item object passed by ItemSend event contains some recipient(s) not only belong to the current email but also from the previously sent one, but they are not visible in the current email, moreover I cannot find such recipients in the sent email as well. The work PC I am using doesn't have debug environment so I am running out of means. Please give me your helps, thanks in advance.

1
Could it be related to the fact that I removed the .vsto file after install? The dll files are copied to appdata/app after install so I think the .vsto file is not useful anymore. - a_pp

1 Answers

0
votes

Could it have anything to do with you doing >

if (countExternalAddress > 0) { Warn(); }

and not ==

if (countExternalAddress == 0)
    {
       Warn();
    }

Seems odd but it would leave one more email to warn you about.