开发者

How to get EmailId from outlook in C#

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜