开发者

How to get EmailId from outlook in C#

开发者 https://www.devze.com 2023-01-07 16:47 出处:网络
I need to get the emailid开发者_如何学Python\'s from outlook in my C# application to send mail. I have userId or FirstName and LastName. Please suggest me a suitable method.

I need to get the emailid开发者_如何学Python's from outlook in my C# application to send mail. I have userId or FirstName and LastName. Please suggest me a suitable method.

Thanks


    public string GetEmaiId(string userId)
    {
        string email = string.Empty;
        DirectorySearcher objsearch = new DirectorySearcher();
        string strrootdse = objsearch.SearchRoot.Path;
        DirectoryEntry objdirentry = new DirectoryEntry(strrootdse);
        objsearch.Filter = "(& (cn=" + userId + ")(objectClass=user))";
        objsearch.SearchScope = System.DirectoryServices.SearchScope.Subtree;
        objsearch.PropertiesToLoad.Add("cn");
        objsearch.PropertyNamesOnly = true;
        objsearch.Sort.Direction = System.DirectoryServices.SortDirection.Ascending;
        objsearch.Sort.PropertyName = "cn";
        SearchResultCollection colresults = objsearch.FindAll();
        foreach (SearchResult objresult in colresults)
        {
            email = objresult.GetDirectoryEntry().Properties["mail"].Value.ToString();
        }
        objsearch.Dispose();
        return email;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号