Resolve primary SMTP address from Exchange Alias (login name)

Hi all,

A bit of background:
Users in our company have multiple emails addresses... all in the form of although there are mutliple different domains for each user, varying dependiong on departments, etc. Unfortunately there is no 'catch-all' domain, so from the userID alone it is not possible to send an email to that user, you need to either use the GAL (i.e. outlook) or know the exact department (and therefore the correct domain) to use.

I need a way to send emails to a user based on their logon details (i.e. from the 'windows authentication' information of an ASP.NET page or whatever), resolving automatically the correct domain of their primary SMTP email address.
Who is Participating?
Dustin HopkinsConnect With a Mentor Managing MemberCommented:
OK...let me get this straight. Do you want to pull the email addr thats stored in ActiveDirectory(Exchange)? If thats the case then you can use this script here: the svcacct and pass and domain are all of the useraccount you are using to search ad, lookupuser is the account, "without" the domain, so KT\dustin would need to be stripped and passed as dustin.
I have a more sreamlined script but I left it at the office, this will pull the first email for the users thats stored in the exchange addressbook. you can change the (0) next to mail to pull the second or third, but you will error if the user doesn't have another stored.
Hope this helps,

Public Function searchad(ByVal lookupuser As String)
        Dim LDAPuser As String = ConfigurationManager.AppSettings("SvcAcct")
        Dim LDAPpass As String = ConfigurationManager.AppSettings("SvcPass")
        Dim LDAPdomain As String = "KT" 'this is the part of the username like KT\dustin
        Dim username As String = LDAPuser
        Dim pwd As String = LDAPpass
        Dim _path As String = "LDAP://"
        'Dim _filterAttribute As String
        Dim domainAndUsername As String = domain & "\" & username
        Dim entry As DirectoryServices.DirectoryEntry = New DirectoryServices.DirectoryEntry(_path, domainAndUsername, pwd)
            Dim obj As Object = entry.NativeObject
            Dim search As DirectoryServices.DirectorySearcher = New DirectoryServices.DirectorySearcher(entry)
            search.Filter = "(SAMAccountName=" & lookupuser & ")"
            Dim result As DirectoryServices.SearchResult = search.FindOne()
            If Not (result Is Nothing) Then
                Dim email As String = result.Properties("mail")(0).ToString()
                Return email
                Return False
            End If
        Catch ex As Exception
            Return False
            Throw New Exception(ex.Message)
        End Try
    End Function

Open in new window

All Courses

From novice to tech pro — start learning today.