如何从Outlook里获得用户的名字和头像

今天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;
        }

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值