Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Exchange Web Services - From Fields Not Populated??

Posted on 2014-02-10
5
Medium Priority
?
1,248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 25

Accepted Solution

by:
Luis Pérez earned 2000 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

2017 Webroot Threat Report

MSPs: Get the facts you need to protect your clients.
The 2017 Webroot Threat Report provides a uniquely insightful global view into the analysis and discoveries made by the Webroot® Threat Intelligence Platform to provide insights on key trends and risks as seen by our users.

Question has a verified solution.

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

Unified and professional email signatures help maintain a consistent company brand image to the outside world. This article shows how to create an email signature in Exchange Server 2010 using a transport rule and how to overcome native limitations …
The core idea of this article is to make you acquainted with the best way in which you can export Exchange mailbox to PST format.
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
how to add IIS SMTP to handle application/Scanner relays into office 365.

660 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