今天share一下如何从outlook里获得缩略图和全名的两个方法。方法的难点是如何从SearchResult 里获得想要的信息。因为这个结果里有大量信息。目前我还没有找到一个很好的方法来处理。找到后我会补全这块的信息。方法仅供参考。
private static string GetThumnailImage(string domain, string alias) { Bitmap thumnailImage = null; DirectorySearcher dirSearcher = new DirectorySearcher(); DirectoryEntry rootEntry = new DirectoryEntry("LDAP://fareast.corp.microsoft.com"); dirSearcher.SearchRoot = rootEntry; dirSearcher.Filter = string.Format("(|(sAMAccountName={0})(cn={0}))", alias); dirSearcher.SearchScope = SearchScope.Subtree; SearchResultCollection searchResultColl = dirSearcher.FindAll(); string imagePath = AppDomain.CurrentDomain.BaseDirectory + "thumbnailphoto.png"; if (searchResultColl.Count <= 0) { throw new System.Security.Authentication.AuthenticationException(); } SearchResult result = searchResultColl[0]; if (result == null || result.Properties == null) { throw new System.Security.Authentication.AuthenticationException(); } if (result.Properties["thumbnailphoto"] != null && result.Properties["thumbnailphoto"].Count > 0) { byte[] imageBytes = new byte[((byte[])result.Properties["thumbnailphoto"][0]).Count<byte>()]; Array.Copy((byte[])result.Properties["thumbnailphoto"][0], imageBytes, imageBytes.Count<byte>()); using (System.IO.MemoryStream mmStream = new System.IO.MemoryStream(imageBytes)) { thumnailImage = new System.Drawing.Bitmap(mmStream); mmStream.Flush(); thumnailImage.Save(imagePath); } } return imagePath; } private static string GetFullName(string domain, string alias) { DirectorySearcher dirSearcher = new DirectorySearcher(); DirectoryEntry rootEntry = new DirectoryEntry("LDAP://fareast.corp.microsoft.com"); dirSearcher.SearchRoot = rootEntry; dirSearcher.Filter = string.Format("(|(sAMAccountName={0})(cn={0}))", alias); dirSearcher.SearchScope = SearchScope.Subtree; SearchResultCollection searchResultColl = dirSearcher.FindAll(); string name = string.Empty; if (searchResultColl.Count <= 0) { throw new System.Security.Authentication.AuthenticationException(); } SearchResult result = searchResultColl[0]; if (result == null || result.Properties == null) { throw new System.Security.Authentication.AuthenticationException(); } if (result.Properties["givenname"] != null && result.Properties["givenname"].Count > 0) { name = result.Properties["givenname"][0].ToString(); } if (result.Properties["sn"] != null && result.Properties["sn"].Count > 0) { name += " " + result.Properties["sn"][0].ToString(); } return name; }