使用别名拉取 Outlook OfficeLocation

Pull Outlook OfficeLocation using Alias

在引用 Return list of names and email address from outlook to vb.net listbox 时,我试图用用户的办公地点填写 ASP:Textbox。

目前我拉取的是当前登录的用户。用户在其 PC 上的用户名也是他们的 Outlook 别名。话虽如此,我正在尝试使用 username/Alias 在 Outlook 中提取 Office 位置。我目前的编码存在以下问题:

'get logged in user(works)
  Dim username As String
  Dim User As System.Security.Principal.IPrincipal
  User = System.Web.HttpContext.Current.User
  username = User.Identity.Name.Substring(3)

'Office Location of User
   Dim itemx As String

'Create an Outlook application.
   Dim oApp As Outlook._Application = New Outlook.Application()

'Get the MAPI namespace.
   Dim oNS As Outlook.NameSpace = oApp.Session
'Get the Global Address List.
   Dim oALs As Outlook.AddressLists = oNS.AddressLists
   Dim oGal As Outlook.AddressList = oALs.Item(1)

'Get all the entries.
   Dim oEntries As Outlook.AddressEntries = oGal.AddressEntries

       For Each entry In oEntries
          If oEntries.GetExchangeUser.Alias = username Then
              itemx = oEntries.GetExchangeUser.OfficeLocation
          End If
       Next

Microsoft 目前不推荐也不支持来自任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT)的 Microsoft Office 应用程序自动化服务),因为当 Office 在此环境中 运行 时,Office 可能会表现出不稳定的行为 and/or 死锁。

如果您要在服务器端上下文中构建 运行 的解决方案,您应该尝试使用已针对无人值守执行安全设置的组件。或者,您应该尝试找到至少允许 运行 客户端部分代码的替代方案。如果您从服务器端解决方案使用 Office 应用程序,该应用程序将缺少许多 运行 成功所必需的功能。此外,您将承担整体解决方案稳定性的风险。在 MSDN 的 Considerations for server-side Automation of Office 文章中阅读更多相关信息。

作为解决方法,您可以考虑使用 EWS,有关详细信息,请参阅 EWS Managed API, EWS, and web services in Exchange。或者只是 Outlook 所基于的低级 API - Extended MAPI.

我了解到的主要事情是您需要密码和用户名才能访问 outlook。

从 PC 获取用户 ID(Outlook 的别名):

 Dim usernameQuery As String
 Dim UserQ As System.Security.Principal.IPrincipal
 UserQ = System.Web.HttpContext.Current.User
 usernameQuery = User.Identity.Name.Substring(3).ToUpper
 UserText.Text = usernameQuery

然后:

像这样调用函数:

'outlook office location **************************************************
    LocationText.Text = GetUserInfo(usernameQuery, "physicaldeliveryofficename")

    'OTHER OPTIONS YOU CAN QUERY
    'Dim svalue As String = GetUserInfo(UserAccount, "mail")
    'Dim svalue As String = GetUserInfo(UserAccount, "givenName")
    'Dim svalue As String = GetUserInfo(UserAccount, "sn")
    'Dim svalue As String = GetUserInfo(UserAccount, "l")
    'Dim svalue As String = GetUserInfo(UserAccount, "st")
    'Dim svalue As String = GetUserInfo(UserAccount, "streetAddress")
    'Dim svalue As String = GetUserInfo(UserAccount, "postalCode")
    'Dim svalue As String = GetUserInfo(UserAccount, "telephoneNumber")
    'Dim svalue As String = GetUserInfo(useraccount, "co")
    'txtName.Text = GetUserInfo(UserAccount, "givenName") & " " & GetUserInfo(UserAccount, "sn")
    'txtPhone.Text = GetUserInfo(UserAccount, "telephoneNumber")
    '********************************************************************************

函数:

Public Function GetUserInfo(ByVal inSAM As String, ByVal inType As String) As String
    Try
        Dim sPath As String = "LDAP://"full_path"/DC="path_value",DC="path_value",DC="path_value" "
        Dim SamAccount As String = Right(inSAM, Len(inSAM) - InStr(inSAM, "\"))
        Dim myDirectory As New DirectoryEntry(sPath, "username", "password") 'pass the user account and password for your Enterprise admin.
        Dim mySearcher As New DirectorySearcher(myDirectory)
        Dim mySearchResultColl As SearchResultCollection
        Dim mySearchResult As SearchResult
        Dim myResultPropColl As ResultPropertyCollection
        Dim myResultPropValueColl As ResultPropertyValueCollection
        'Build LDAP query
        mySearcher.Filter = ("(&(objectClass=user)(samaccountname=" & SamAccount & "))")
        mySearchResultColl = mySearcher.FindAll()
        'I expect only one user from search result
        Select Case mySearchResultColl.Count
            Case 0
                Return "Null"
                Exit Function
            Case Is > 1
                Return "Null"
                Exit Function
        End Select

        'Get the search result from the collection
        mySearchResult = mySearchResultColl.Item(0)

        ''Get the Properites, they contain the usefull info
        myResultPropColl = mySearchResult.Properties

        If myResultPropColl.Contains(inType) Then
            myResultPropValueColl = myResultPropColl.Item(inType)
            Return CStr(myResultPropValueColl.Item(0))
        End If

        'displayname, mail
        'Retrieve from the properties collection the display name and email of the user
        'myResultPropValueColl = myResultPropColl.Item(inType)
        'Return CStr(myResultPropValueColl.Item(0))

    Catch ex As System.Exception

    End Try
    Return "Null"
End Function

仅供参考-

full_path = "value"."value"."value"