Outlook插件访问addressEntry.AddressEntryUserType时的内部支持功能错误

时间:2018-04-06 21:17:25

标签: c# outlook-addin

我试图找出导致Outlook插件崩溃的原因。我们发现了这个错误:

System.Runtime.InteropServices.COMException (0x8E640201): An internal support function returned an error.
   at Microsoft.Office.Interop.Outlook.AddressEntry.get_AddressEntryUserType()
   at DropSendOutlook.RecipientAddressGetter.GetSmtpAddress(AddressEntry addressEntry)
   at DropSendOutlook.RecipientAddressGetter.<Get>b__0(Recipient recipient)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.<DistinctIterator>d__64`1.MoveNext()
   at System.String.Join(String separator, IEnumerable`1 values)
   at DropSendOutlook.RecipientAddressGetter.Get(MailItem mail)
   at DropSendOutlook.ThisAddIn.outlookApp_ItemSend(Object item, Boolean& cancel)

根据这个堆栈跟踪,很明显问题出在这个函数中:

private static string GetSmtpAddress(AddressEntry addressEntry)
{
    var addressEntryUserType = addressEntry.AddressEntryUserType;
    Log.Info(addressEntryUserType);
    //Now we have an AddressEntry representing the Sender
    if (addressEntryUserType == OlAddressEntryUserType.olExchangeUserAddressEntry
        || addressEntryUserType == OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
    {
        //Use the ExchangeUser object PrimarySMTPAddress
        ExchangeUser exchUser = addressEntry.GetExchangeUser();
        return exchUser != null ? exchUser.PrimarySmtpAddress : GetSmtpAddressExchangeOld(addressEntry);
    }
    if (addressEntryUserType == OlAddressEntryUserType.olSmtpAddressEntry)
    {
        return addressEntry.Address;
    }
    if (addressEntryUserType == OlAddressEntryUserType.olOutlookContactAddressEntry)
    {
        // Could throw System.Runtime.InteropServices.COMException (0x8004010F), possible reasons with permissions.
        try
        {
            try
            {
                var contact = addressEntry.GetContact();

                if (contact != null && contact.Email1AddressType == "EX")
                {
                    var currentDisplayName = contact.Email1DisplayName;
                    contact.Email1DisplayName = string.Empty;
                    var separators = new[] { '(', ')' };
                    var eMailParse = contact.Email1DisplayName.Split(separators);
                    contact.Email1DisplayName = currentDisplayName;
                    return eMailParse[1];
                }
            }
            catch
            {
                var ms = new FrmMessage();
                ms.SetMessage("Contact was not found!");
                ms.Show();
                throw;
            }
        }
        catch (System.Exception ex)
        {
            Log.Error(ex.ToString());
        }
        return addressEntry.Address;
    }
    if (addressEntryUserType == OlAddressEntryUserType.olExchangeDistributionListAddressEntry)
    {
        var exchDistribution = addressEntry.GetExchangeDistributionList();
        return exchDistribution != null
            ? exchDistribution.PrimarySmtpAddress
            : GetSmtpAddressExchangeOld(addressEntry);
    }
    if (addressEntryUserType == OlAddressEntryUserType.olOutlookDistributionListAddressEntry)
    {
        var addresses = from AddressEntry member in addressEntry.Members select GetSmtpAddress(member);
        return string.Join(";", addresses);
    }
    return addressEntry.Address;
}

似乎特意来自这条线:

var addressEntryUserType = addressEntry.AddressEntryUserType;

造成这种情况的原因是什么?在访问AddressEntryUserType之前是否应该进行某种检查以避免此错误?

0 个答案:

没有答案