简体   繁体   English

如何使用C#以编程方式向Active Directory中的用户设置电子邮件

[英]How to set email to a user in Active Directory programatically using c#

I have programatically created a user in Active Directory. 我已经通过编程在Active Directory中创建了一个用户。 I want to set that user's email. 我要设置该用户的电子邮件。 I used the following code to set email address in active directory: 我使用以下代码在活动目录中设置电子邮件地址:

UserDirectoryEntry.Properties["mail"].Value = "john@gmail.com";
UserDirectoryEntry.CommitChanges();

At this point, the email address gets set in active directory properly. 此时,电子邮件地址已正确设置在活动目录中。 But the moment, i try to update another property for that user say for eg: 但是此刻,我尝试为该用户更新另一个属性,例如:

UserDirectoryEntry.Properties["telephoneNumber"].value = "022-2345678";
UserDirectoryEntry.CommitChanges();

After this point, the email address that we set earlier to "mail" field gets replaced with SamAccountName@domainName.com. 此后,我们之前设置为“ mail”字段的电子邮件地址将替换为SamAccountName@domainName.com。

Can you please tell me why the email address is getting replaced with SamAccountName@domainName.com even if i have set it to "john@gmail.com". 您能否告诉我,即使我将其设置为“ john@gmail.com”,为什么电子邮件地址仍被SamAccountName@domainName.com代替。 I m using APIs of Active Directory. 我正在使用Active Directory的API。

I do not know if this works for you, but you can try adding the email instead of setting it: 我不知道这是否适合您,但是您可以尝试添加电子邮件,而不是对其进行设置:

UserDirectoryEntry.Properties["mail"].Add("john@gmail.com");

You will be still able to find the user bu this email. 您仍然可以通过此电子邮件找到该用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM