locked
parsing InternetMessageHeaders via EWS RRS feed

  • Question

  • I have some generic mailboxes in my Exchange 2010 environment that have a number of smtp addresses assigned to them.  I would like to write a script to parse the emails in these generic mailboxes and grab the smtp address that the email was sent to.  I can do this manually by opening the email in outlook and looking at the Internet Message Header, the 'To' field shows the specific smtp address the email was addressed to.  When I connect to the mailboxes via EWS and parse the InternetMessageHeader of the email, I can see all the values except for the 'from' and 'to' fields.  

    The ToRecipients property of the email itself won't work for me as it simply shows the primary smtp address of the mailbox.  

    Friday, August 22, 2014 11:21 PM

Answers

  • I'd suggest you try using the PR_TRANSPORT_MESSAGE_HEADERS extended property you can have a read of http://msdn.microsoft.com/EN-US/library/office/hh545614(v=exchg.140).aspx which explains why using the Strongly typed properties can be inconstant. eg something like this should work

    ## Get the Mailbox to Access from the 1st commandline argument
    
    $MailboxName = $args[0]
    
    ## Load Managed API dll  
    
    ###CHECK FOR EWS MANAGED API, IF PRESENT IMPORT THE HIGHEST VERSION EWS DLL, ELSE EXIT
    $EWSDLL = (($(Get-ItemProperty -ErrorAction SilentlyContinue -Path Registry::$(Get-ChildItem -ErrorAction SilentlyContinue -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Exchange\Web Services'|Sort-Object Name -Descending| Select-Object -First 1 -ExpandProperty Name)).'Install Directory') + "Microsoft.Exchange.WebServices.dll")
    if (Test-Path $EWSDLL)
        {
        Import-Module $EWSDLL
        }
    else
        {
        "$(get-date -format yyyyMMddHHmmss):"
        "This script requires the EWS Managed API 1.2 or later."
        "Please download and install the current version of the EWS Managed API from"
        "http://go.microsoft.com/fwlink/?LinkId=255472"
        ""
        "Exiting Script."
        exit
        }
      
    ## Set Exchange Version  
    $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2  
      
    ## Create Exchange Service Object  
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)  
      
    ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials  
      
    #Credentials Option 1 using UPN for the windows Account  
    $psCred = Get-Credential  
    $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())  
    $service.Credentials = $creds      
      
    #Credentials Option 2  
    #service.UseDefaultCredentials = $true  
      
    ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates  
      
    ## Code From http://poshcode.org/624
    ## Create a compilation environment
    $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
    $Compiler=$Provider.CreateCompiler()
    $Params=New-Object System.CodeDom.Compiler.CompilerParameters
    $Params.GenerateExecutable=$False
    $Params.GenerateInMemory=$True
    $Params.IncludeDebugInformation=$False
    $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
    
    $TASource=@'
      namespace Local.ToolkitExtensions.Net.CertificatePolicy{
        public class TrustAll : System.Net.ICertificatePolicy {
          public TrustAll() { 
          }
          public bool CheckValidationResult(System.Net.ServicePoint sp,
            System.Security.Cryptography.X509Certificates.X509Certificate cert, 
            System.Net.WebRequest req, int problem) {
            return true;
          }
        }
      }
    '@ 
    $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
    $TAAssembly=$TAResults.CompiledAssembly
    
    ## We now create an instance of the TrustAll and attach it to the ServicePointManager
    $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
    [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
    
    ## end code from http://poshcode.org/624
      
    ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use  
      
    #CAS URL Option 1 Autodiscover  
    $service.AutodiscoverUrl($MailboxName,{$true})  
    "Using CAS Server : " + $Service.url   
       
    #CAS URL Option 2 Hardcoded  
      
    #$uri=[system.URI] "https://casservername/ews/exchange.asmx"  
    #$service.Url = $uri    
      
    ## Optional section for Exchange Impersonation  
      
    #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName) 
    
    # Bind to the Inbox Folder
    $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)   
    $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
    $PR_TRANSPORT_MESSAGE_HEADERS = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x007D,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);
    $psPropset= new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly)  
    $psPropset.Add($PR_TRANSPORT_MESSAGE_HEADERS)
    $psPropset.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject)
    #Define ItemView to retrive just 1000 Items    
    $ivItemView =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)  
    $ivItemView.PropertySet = $psPropset
    $rptCollection = @()
    $fiItems = $null    
    do{    
        $fiItems = $service.FindItems($Inbox.Id,$ivItemView)    
        [Void]$service.LoadPropertiesForItems($fiItems,$psPropset)  
        foreach($Item in $fiItems.Items){    
    		$Headers = $null;
    		if($Item.TryGetProperty($PR_TRANSPORT_MESSAGE_HEADERS,[ref]$Headers)){
    			$slen = $Headers.ToLower().IndexOf("`nto: ")
    			if($slen -gt 0){
    				$elen = $Headers.IndexOf("`r`n",$slen)
    				Write-Host ("Subject : " + $Item.Subject)
    				write-host ("To : " + $Headers.SubString(($slen+4),$elen-($slen+4)))
    			}
    		}      
        }    
        $ivItemView.Offset += $fiItems.Items.Count
    	Write-Host ("Processed " + $ivItemView.Offset + " of " + $fiItems.TotalCount)
    }while($fiItems.MoreAvailable -eq $true) 
    Cheers
    Glen

     
    • Edited by Glen ScalesMVP Monday, August 25, 2014 4:49 AM
    • Marked as answer by pveerman Tuesday, August 26, 2014 2:53 PM
    Monday, August 25, 2014 4:49 AM

All replies

  • I'd suggest you try using the PR_TRANSPORT_MESSAGE_HEADERS extended property you can have a read of http://msdn.microsoft.com/EN-US/library/office/hh545614(v=exchg.140).aspx which explains why using the Strongly typed properties can be inconstant. eg something like this should work

    ## Get the Mailbox to Access from the 1st commandline argument
    
    $MailboxName = $args[0]
    
    ## Load Managed API dll  
    
    ###CHECK FOR EWS MANAGED API, IF PRESENT IMPORT THE HIGHEST VERSION EWS DLL, ELSE EXIT
    $EWSDLL = (($(Get-ItemProperty -ErrorAction SilentlyContinue -Path Registry::$(Get-ChildItem -ErrorAction SilentlyContinue -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Exchange\Web Services'|Sort-Object Name -Descending| Select-Object -First 1 -ExpandProperty Name)).'Install Directory') + "Microsoft.Exchange.WebServices.dll")
    if (Test-Path $EWSDLL)
        {
        Import-Module $EWSDLL
        }
    else
        {
        "$(get-date -format yyyyMMddHHmmss):"
        "This script requires the EWS Managed API 1.2 or later."
        "Please download and install the current version of the EWS Managed API from"
        "http://go.microsoft.com/fwlink/?LinkId=255472"
        ""
        "Exiting Script."
        exit
        }
      
    ## Set Exchange Version  
    $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2  
      
    ## Create Exchange Service Object  
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)  
      
    ## Set Credentials to use two options are availible Option1 to use explict credentials or Option 2 use the Default (logged On) credentials  
      
    #Credentials Option 1 using UPN for the windows Account  
    $psCred = Get-Credential  
    $creds = New-Object System.Net.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())  
    $service.Credentials = $creds      
      
    #Credentials Option 2  
    #service.UseDefaultCredentials = $true  
      
    ## Choose to ignore any SSL Warning issues caused by Self Signed Certificates  
      
    ## Code From http://poshcode.org/624
    ## Create a compilation environment
    $Provider=New-Object Microsoft.CSharp.CSharpCodeProvider
    $Compiler=$Provider.CreateCompiler()
    $Params=New-Object System.CodeDom.Compiler.CompilerParameters
    $Params.GenerateExecutable=$False
    $Params.GenerateInMemory=$True
    $Params.IncludeDebugInformation=$False
    $Params.ReferencedAssemblies.Add("System.DLL") | Out-Null
    
    $TASource=@'
      namespace Local.ToolkitExtensions.Net.CertificatePolicy{
        public class TrustAll : System.Net.ICertificatePolicy {
          public TrustAll() { 
          }
          public bool CheckValidationResult(System.Net.ServicePoint sp,
            System.Security.Cryptography.X509Certificates.X509Certificate cert, 
            System.Net.WebRequest req, int problem) {
            return true;
          }
        }
      }
    '@ 
    $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
    $TAAssembly=$TAResults.CompiledAssembly
    
    ## We now create an instance of the TrustAll and attach it to the ServicePointManager
    $TrustAll=$TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
    [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll
    
    ## end code from http://poshcode.org/624
      
    ## Set the URL of the CAS (Client Access Server) to use two options are availbe to use Autodiscover to find the CAS URL or Hardcode the CAS to use  
      
    #CAS URL Option 1 Autodiscover  
    $service.AutodiscoverUrl($MailboxName,{$true})  
    "Using CAS Server : " + $Service.url   
       
    #CAS URL Option 2 Hardcoded  
      
    #$uri=[system.URI] "https://casservername/ews/exchange.asmx"  
    #$service.Url = $uri    
      
    ## Optional section for Exchange Impersonation  
      
    #$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName) 
    
    # Bind to the Inbox Folder
    $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)   
    $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
    $PR_TRANSPORT_MESSAGE_HEADERS = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x007D,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);
    $psPropset= new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly)  
    $psPropset.Add($PR_TRANSPORT_MESSAGE_HEADERS)
    $psPropset.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::Subject)
    #Define ItemView to retrive just 1000 Items    
    $ivItemView =  New-Object Microsoft.Exchange.WebServices.Data.ItemView(1000)  
    $ivItemView.PropertySet = $psPropset
    $rptCollection = @()
    $fiItems = $null    
    do{    
        $fiItems = $service.FindItems($Inbox.Id,$ivItemView)    
        [Void]$service.LoadPropertiesForItems($fiItems,$psPropset)  
        foreach($Item in $fiItems.Items){    
    		$Headers = $null;
    		if($Item.TryGetProperty($PR_TRANSPORT_MESSAGE_HEADERS,[ref]$Headers)){
    			$slen = $Headers.ToLower().IndexOf("`nto: ")
    			if($slen -gt 0){
    				$elen = $Headers.IndexOf("`r`n",$slen)
    				Write-Host ("Subject : " + $Item.Subject)
    				write-host ("To : " + $Headers.SubString(($slen+4),$elen-($slen+4)))
    			}
    		}      
        }    
        $ivItemView.Offset += $fiItems.Items.Count
    	Write-Host ("Processed " + $ivItemView.Offset + " of " + $fiItems.TotalCount)
    }while($fiItems.MoreAvailable -eq $true) 
    Cheers
    Glen

     
    • Edited by Glen ScalesMVP Monday, August 25, 2014 4:49 AM
    • Marked as answer by pveerman Tuesday, August 26, 2014 2:53 PM
    Monday, August 25, 2014 4:49 AM
  • Thanks Glen!  Loading the PR_TRANSPORT_MESSAGE_HEADERS did the trick.

    Tuesday, August 26, 2014 2:59 PM