Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1280
  • Last Modified:

Exchange Web Services - From Fields Not Populated??

Hi,
I recently downloaded the Exchange Web Services SDK and have used it to create a small application who's aim is to extract data regarding email usage.  One of the fields I am interested in is the sender and in particular I need to get the Sender's email address.  Most of the other fields seem to contain what I want but anything that involves email addresses seems to be set to nothing yet the name field holds the sender's name.  Here is the bare bones of what I am doing:
Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Net
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates
Imports Microsoft.Exchange.WebServices.Data

        Dim Str As String = ""
        Dim l As Integer = 0
        Dim parentFolderName As WellKnownFolderName
        Dim view As New ItemView(1000)
        Dim returnValue As FindItemsResults(Of Item)
        Dim x As Integer = 0
        Dim n As Integer = 0
        Dim service As ExchangeService = Nothing
       
         service = New ExchangeService(ExchangeVersion.Exchange2010)
         ServicePointManager.ServerCertificateValidationCallback = AddressOf CertificateValidationCallBack

         service.Credentials = New WebCredentials(A.UserName, A.Password, A.Domain)

         service.AutodiscoverUrl(A.EmailAddress)

         parentFolderName = WellKnownFolderName.Inbox

         If returnValue.Items.Count > 0 Then
               For j = 0 To returnValue.Items.Count - 1
                         Str = DirectCast(returnValue.Items(j), Microsoft.Exchange.WebServices.Data.EmailMessage).From.Address

                        If Str = "" Then
                            lstResults.Items(l).SubItems.Add("-")
                        Else
                            lstResults.Items(l).SubItems.Add(Str)
                        End If
                        l += 1
              Next
        End if


When I interrogate the " DirectCast(returnValue.Items(j), Microsoft.Exchange.WebServices.Data.EmailMessage).From.Address" code the address is always nothing, if I look at:

 DirectCast(returnValue.Items(j), Microsoft.Exchange.WebServices.Data.EmailMessage).From.name

That has the name of the sender which is not what I want, can anyone explain what I need to do to get the email address of the sender.  I am also getting the same with the CC and BCC fields they are always empty even when there are actual CC and BCCs in the inbox email message I am interrogating.

Siv
0
Siv
Asked:
Siv
  • 3
  • 2
1 Solution
 
Luis PérezSoftware Architect in .NetCommented:
Try the following:

For j = 0 To returnValue.Items.Count - 1
    Dim messageID As String = returnValue.Items(j).Id.UniqueId
    Dim realMessage As EmailMessage = EmailMessage.Bind(service, New Microsoft.Exchange.WebServices.Data.ItemId(messageID))
    'Now look if you can get the Address from realMessage.From.Address
    'If not yet, then add the following lines:
    Dim pSet As PropertySet = New PropertySet(BasePropertySet.FirstClassProperties)
    realMessage.Load(pSet)
    'And then check again if you can get realMessage.From.Address
Next

Open in new window

Hope that helps.
0
 
SivAuthor Commented:
Luis,

Thanks very much that did the trick, would you mind explaining what that extra step of binding the service actually does and why it's needed as I would really like to understand why that works?

It's not obvious from the SDK (or at least the bits of it I have looked at so far), why I would need to do that.

Again, thanks very much for your help.

Siv
0
 
SivAuthor Commented:
This example worked immediately and solved my problem.
Thanks very much Luis.
0
 
Luis PérezSoftware Architect in .NetCommented:
Well, I don't know *exactly* why performing the search doesn't bring the e-mail addresses. But I remember from a project in which I worked some time ago that I had the same problem: I needed not only the From address, but many more fields that didn't come in the results. The only thing I can suppose is that the SDK only gets some fields in the results in order to make the search faster.

So, binding the complete message (well, not really complete, because some fields are not charged even with EmailMessage.Bind if you don't add them to the PropertySet) is going one step further, and then many more fields of the EmailMessage are filled.

Hope that helps, although I know this explanation is so poor.
0
 
SivAuthor Commented:
Luis,
Thanks for taking the time to reply, it all helps.

With your additional pieces of code it has made my code a lot less long-winded as I can refer to the items I want as "Str = RealMessage.From.Address" rather than having to write

Str = DirectCast(returnValue.Items(j), Microsoft.Exchange.WebServices.Data.EmailMessage).From.Address

So my code is a bit easier to read as well.

Thanks again.
Siv
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now