Solved

Exchange Web Services - From Fields Not Populated??

Posted on 2014-02-10
5
1,124 Views
Last Modified: 2014-02-11
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
Comment
Question by:Siv
  • 3
  • 2
5 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 500 total points
ID: 39850209
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
 

Author Comment

by:Siv
ID: 39850518
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
 

Author Closing Comment

by:Siv
ID: 39850521
This example worked immediately and solved my problem.
Thanks very much Luis.
0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 39850540
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
 

Author Comment

by:Siv
ID: 39850679
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

Want to promote your upcoming event?

Are you going to an event? Are you going to be exhibiting at a tradeshow? Talking at a conference? Using a promotional banner in your email signature ensures that your organization’s most important contacts stay in the know and can potentially spread the word about the event.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this video we show how to create a Shared Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Sha…
To show how to create a transport rule in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Rules tab.:  To cr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now