public static void MailDisable(GenericDirectoryObject directoryObject) { try { directoryObject.EmailAddress = null; directoryObject.TargetAddress = null; directoryObject.Save(); } catch { directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context, directoryObject.DistinguishedName); } try { directoryObject.MailNickname = null; directoryObject.Save(); } catch { directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context, directoryObject.DistinguishedName); } var proxyAddresses = new List<string>(); try { proxyAddresses.AddRange( directoryObject.ProxyAddresses.Where( address => address.Substring(0, 5).ToLowerInvariant() != "smtp:")); } catch { } try { if (proxyAddresses.Count > 0) { directoryObject.ProxyAddresses = proxyAddresses; directoryObject.Save(); } else { directoryObject.SetAttribute("proxyAddresses", null); directoryObject.Save(); } } catch { directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context, directoryObject.DistinguishedName); } try { directoryObject.SetAttribute("showInAddressBook", null); directoryObject.Save(); } catch { directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context, directoryObject.DistinguishedName); } try { directoryObject.LegacyExchangeDn = null; directoryObject.Save(); } catch { directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context, directoryObject.DistinguishedName); } }
public static void AddAlias(GenericDirectoryObject directoryObject, string address, bool makeDefault) { var handle = address.Split(new[] {'@'})[0]; var proxyAddresses = new List<string>(); var nicknameProxies = false; if (makeDefault) { try { directoryObject.EmailAddress = address.Trim(); directoryObject.TargetAddress = string.Format("smtp:{0}", address); if (string.IsNullOrWhiteSpace(directoryObject.MailNickname)) { directoryObject.MailNickname = handle; nicknameProxies = true; } directoryObject.Save(); } catch { directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context, directoryObject.DistinguishedName); } } try { proxyAddresses = directoryObject.ProxyAddresses; } catch { } var newProxyAddresses = new List<string>(); foreach (var proxyAddress in proxyAddresses) { if (proxyAddress.Substring(0, 5).ToLowerInvariant() == "smtp:") { var s = proxyAddress.Split(new[] {':'}); if (s[1].ToLowerInvariant() == address.ToLowerInvariant()) { newProxyAddresses.Add(makeDefault ? string.Format("SMTP:{0}", address) : string.Format("smtp:{0}", address)); } else { newProxyAddresses.Add(proxyAddress.Replace("SMTP", "smtp")); } } else { newProxyAddresses.Add(proxyAddress); } } if (nicknameProxies) { newProxyAddresses.AddRange( Config.RoutingDomains.Select(domain => string.Format("smtp:{0}@{1}", handle, domain))); } try { directoryObject.SetAttribute("proxyAddresses", null); directoryObject.Save(); } catch { } finally { directoryObject = GenericDirectoryObject.FindByIdentity(directoryObject.Context, directoryObject.DistinguishedName); } directoryObject.ProxyAddresses = newProxyAddresses; directoryObject.Save(); }