简体   繁体   中英

EWS - Get current user (sender) contact information vb.net

I'm currently developping a program that will serve to send emails via company's EWS. the code for sending the message works perfectly but i also need to get some data about the sender of the email. It means, when a user sends the email to me, I need to see his position and address. I'm struggling for more then a week to find a way to define the sender in the code and his contact details. And still nothing found so far. Will appreacite your helf.

My code so far:

Dim url As String = "https://.../ews/Exchange.asmx"
    exch.Url = New System.Uri(url)
    exch.UseDefaultCredentials = False
    exch.Credentials = New System.Net.NetworkCredential(TextBox2.Text, TextBox1.Text)
    ' exch.AutodiscoverUrl("myemail")
    'exch.ResolveName("", ResolveNameSearchLocation.ContactsThenDirectory, True)
    Dim message As New EmailMessage(exch)
    message.Subject = "Новое заявление (АП) - " & ComboBox1.SelectedItem
    message.Body = "Добрый день!" & vbNewLine & vbNewLine & "Прошу обработать заявление - " & ComboBox1.SelectedItem
    For Each f In attfiles
        message.Attachments.AddFileAttachment(f)
    Next
    message.ToRecipients.Add(email)
    message.SendAndSaveCopy()

You are on the correct path by using the ResolveNames operation. Using the sender SMTP address, use ResolveNames to get back a list of potential matches for the sender. The foreach in the example is just so you can see each result. Since you are passing an SMTP address, it is very likely that your result set may be no more than a few contacts.

It sounds like one of your assumptions is that the sender always has an entry in the user's Contacts folder. Is that always true? Can the sender not exist as an entries in the recipients Contact folder but have an entry in Active Directory? You are doing the right thing to cover both possibilities by using the ResolveNameSearchLocation.ContactsThenDirectory option.

Resolve names works well if you have a display name or SMTP address. You also ask about how to find a specific contact. You mention that want to search the Contacts folder, but you are concerned about the number of employees. Do all employees have contact items in the target mailboxes? To search for specific contacts, learn about EWS search .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM