public static string GetEmailAdressOrNull (AddressEntry addressEntry, IEntityMappingLogger logger, ILog generalLogger)
    {
      OlAddressEntryUserType type;

      if (addressEntry != null)
      {
        try
        {
          type = addressEntry.AddressEntryUserType;
        }
        catch (COMException ex)
        {
          generalLogger.Warn ("Could not get type from AddressEntry", ex);
          logger.LogMappingWarning ("Could not get type from AddressEntry", ex);
          return null;
        }
        if (type == OlAddressEntryUserType.olExchangeUserAddressEntry
            || type == OlAddressEntryUserType.olExchangeRemoteUserAddressEntry
            || type == OlAddressEntryUserType.olExchangeAgentAddressEntry
            || type == OlAddressEntryUserType.olExchangeOrganizationAddressEntry
            || type == OlAddressEntryUserType.olExchangePublicFolderAddressEntry)
        {
          try
          {
            using (var exchUser = GenericComObjectWrapper.Create (addressEntry.GetExchangeUser ()))
            {
              if (exchUser.Inner != null)
              {
                return exchUser.Inner.PrimarySmtpAddress;
              }
            }
          }
          catch (COMException ex)
          {
            generalLogger.Warn ("Could not get email address from adressEntry.GetExchangeUser()", ex);
            logger.LogMappingWarning ("Could not get email address from adressEntry.GetExchangeUser()", ex);
          }
        }
        else if (type == OlAddressEntryUserType.olExchangeDistributionListAddressEntry
                 || type == OlAddressEntryUserType.olOutlookDistributionListAddressEntry)
        {
          try
          {
            using (var exchDL = GenericComObjectWrapper.Create (addressEntry.GetExchangeDistributionList ()))
            {
              if (exchDL.Inner != null)
              {
                return exchDL.Inner.PrimarySmtpAddress;
              }
            }
          }
          catch (COMException ex)
          {
            generalLogger.Warn ("Could not get email address from adressEntry.GetExchangeDistributionList()", ex);
            logger.LogMappingWarning ("Could not get email address from adressEntry.GetExchangeDistributionList()", ex);
          }
        }
        else if (type == OlAddressEntryUserType.olSmtpAddressEntry
                 || type == OlAddressEntryUserType.olLdapAddressEntry)
        {
          return addressEntry.Address;
        }
        else if (type == OlAddressEntryUserType.olOutlookContactAddressEntry)
        {
          if (addressEntry.Type == "EX")
          {
            try
            {
              using (var exchContact = GenericComObjectWrapper.Create (addressEntry.GetContact ()))
              {
                if (exchContact.Inner != null)
                {
                  if (exchContact.Inner.Email1AddressType == "EX")
                  {
                    return exchContact.Inner.GetPropertySafe (PR_EMAIL1ADDRESS);
                  }
                  else
                  {
                    return exchContact.Inner.Email1Address;
                  }
                }
              }
            }
            catch (COMException ex)
            {
              generalLogger.Warn ("Could not get email address from adressEntry.GetContact()", ex);
              logger.LogMappingWarning ("Could not get email address from adressEntry.GetContact()", ex);
            }
          }
          else
          {
            return addressEntry.Address;
          }
        }
        else
        {
          try
          {
            return addressEntry.GetPropertySafe (PR_SMTP_ADDRESS);
          }
          catch (COMException ex)
          {
            generalLogger.Warn ("Could not get property PR_SMTP_ADDRESS for adressEntry", ex);
            logger.LogMappingWarning ("Could not get property PR_SMTP_ADDRESS for adressEntry", ex);
          }
        }
      }

      return null;
    }
 public static AddressEntry GetEventOrganizerOrNull (AppointmentItem source, IEntityMappingLogger logger, ILog generalLogger, int outlookMajorVersion)
 {
   try
   {
     if (outlookMajorVersion < 14)
     {
       // Microsoft recommends this way for Outlook 2007. May still work with Outlook 2010+
       using (var propertyAccessor = GenericComObjectWrapper.Create (source.PropertyAccessor))
       {
         string organizerEntryID = propertyAccessor.Inner.BinaryToString (propertyAccessor.Inner.GetProperty(PR_SENT_REPRESENTING_ENTRYID));
         return Globals.ThisAddIn.Application.Session.GetAddressEntryFromID (organizerEntryID);
       }
     }
     else
     {
       // NB this works with Outlook 2010 but crashes with Outlook 2007
       return source.GetOrganizer();
     }
   }
   catch (COMException ex)
   {
     generalLogger.Warn ("Can't get organizer of appointment", ex);
     logger.LogMappingWarning ("Can't get organizer of appointment", ex);
     return null;
   }
 }
    public static void MapCustomProperties2To1 (ICalendarPropertyList sourceList, GenericComObjectWrapper<UserProperties> userPropertiesWrapper, bool mapAllCustomProperties, PropertyMapping[] mappings, IEntityMappingLogger logger, ILog s_logger)
    {
      var alreadyMappedOutlookProperties = new HashSet<string>();

      foreach (var mapping in mappings)
      {
        var prop = sourceList.FirstOrDefault (p => p.Name == mapping.DavProperty);
        if (prop != null)
        {
          try
          {
            alreadyMappedOutlookProperties.Add(mapping.OutlookProperty);
            using (var userProperty = GenericComObjectWrapper.Create (userPropertiesWrapper.Inner.Find (mapping.OutlookProperty)))
            {
              if (userProperty.Inner != null)
              {
                userProperty.Inner.Value = prop.Value;
              }
              else
              {
                using (var newUserProperty = GenericComObjectWrapper.Create (userPropertiesWrapper.Inner.Add (mapping.OutlookProperty, OlUserPropertyType.olText, true)))
                {
                  newUserProperty.Inner.Value = prop.Value;
                }
              }
            }
          }
          catch (COMException ex)
          {
            s_logger.Warn ("Can't set UserProperty of Item!", ex);
            logger.LogMappingWarning ("Can't set UserProperty of Item!", ex);
          }
        }
      }
      if (mapAllCustomProperties)
      {
        foreach (var prop in sourceList.Where(p => p.Name.StartsWith("X-CALDAVSYNCHRONIZER-")))
        {
          var outlookProperty = prop.Name.Replace("X-CALDAVSYNCHRONIZER-", "");
          if (!alreadyMappedOutlookProperties.Contains(outlookProperty))
          {
            try
            {
              using (var userProperty = GenericComObjectWrapper.Create(userPropertiesWrapper.Inner.Find(outlookProperty)))
              {
                if (userProperty.Inner != null)
                {
                  userProperty.Inner.Value = prop.Value;
                }
                else
                {
                  using (var newUserProperty = GenericComObjectWrapper.Create(userPropertiesWrapper.Inner.Add(outlookProperty, OlUserPropertyType.olText, true)))
                  {
                    newUserProperty.Inner.Value = prop.Value;
                  }
                }
              }
            }
            catch (COMException ex)
            {
              s_logger.Warn("Can't set UserProperty of Item!", ex);
              logger.LogMappingWarning("Can't set UserProperty of Item!", ex);
            }
          }
        }
      }
    }
 public static string GetSenderEmailAddressOrNull (AppointmentItem source, IEntityMappingLogger logger, ILog generalLogger)
 {
   try
   {
     return source.GetPropertySafe (PR_SENDER_EMAIL_ADDRESS);
   }
   catch (COMException ex)
   {
     generalLogger.Warn ("Can't access property PR_SENDER_EMAIL_ADDRESS of appointment", ex);
     logger.LogMappingWarning ("Can't access property PR_SENDER_EMAIL_ADDRESS of appointment", ex);
     return null;
   }
 }
 public static void MapCustomProperties1To2 (GenericComObjectWrapper<UserProperties> userPropertiesWrapper, ICalendarPropertyList targetProperties, bool mapAllCustomProperties, PropertyMapping[] mappings, IEntityMappingLogger logger, ILog s_logger)
 {
   if (userPropertiesWrapper.Inner != null && userPropertiesWrapper.Inner.Count > 0)
   {
     foreach (var prop in userPropertiesWrapper.Inner.ToSafeEnumerable<UserProperty>())
     {
       try
       {
         if (prop.Value != null && !string.IsNullOrEmpty (prop.Value.ToString()) && (prop.Type == OlUserPropertyType.olText))
         {
           var foundMapping = mappings.FirstOrDefault (m => m.OutlookProperty == prop.Name);
           if (foundMapping != null)
           {
             targetProperties.Add (new CalendarProperty (foundMapping.DavProperty, prop.Value.ToString()));
           }
           else if (mapAllCustomProperties)
           {
             targetProperties.Add (new CalendarProperty ("X-CALDAVSYNCHRONIZER-" + prop.Name, prop.Value.ToString()));
           }
         }
       }
       catch (COMException ex)
       {
         s_logger.Warn ("Can't access UserProperty of Item!", ex);
         logger.LogMappingWarning ("Can't access UserProperty of Item!", ex);
       }
     }
   }
 }