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 UserID@domain.com... 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.
LVL 1
SamShawAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dustin HopkinsSenior Web DeveloperCommented:
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,

Dustin
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://kt.dusion.com"
        'Dim _filterAttribute As String
        Dim domainAndUsername As String = domain & "\" & username
        Dim entry As DirectoryServices.DirectoryEntry = New DirectoryServices.DirectoryEntry(_path, domainAndUsername, pwd)
        Try
 
            Dim obj As Object = entry.NativeObject
            Dim search As DirectoryServices.DirectorySearcher = New DirectoryServices.DirectorySearcher(entry)
            search.Filter = "(SAMAccountName=" & lookupuser & ")"
            search.PropertiesToLoad.Add("SAMAccountName")
            search.PropertiesToLoad.Add("cn")
            search.PropertiesToLoad.Add("mail")
            search.PropertiesToLoad.Add("physicalDeliveryOfficeName")
            search.PropertiesToLoad.Add("telephoneNumber")
            Dim result As DirectoryServices.SearchResult = search.FindOne()
 
            If Not (result Is Nothing) Then
                Dim email As String = result.Properties("mail")(0).ToString()
                Return email
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
            Throw New Exception(ex.Message)
 
        End Try
    End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.