Solved

Exchange Web Services - From Fields Not Populated??

Posted on 2014-02-10
5
1,139 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
write xml in vb.net 2 24
Issue with mobile devices syncing with Exchange 2013 server 2 48
VB.Net - Get ColumnNames of SQL Table 4 11
Exchange 2007 3 32
Lotus Notes – formerly IBM Notes – is an email client application, while IBM Domino (earlier Lotus Domino) is an email server. The client possesses a set of features that are even more advanced as compared to that of Outlook. Likewise, IBM Domino is…
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
In this video we show how to create a Contact 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 >> Contact ta…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

776 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