Despatched and Obtained Time on a Message and EWS

Despatched and Obtained Time on a Message and EWS This one has come up a few instances for me over the past couple of weeks so I assumed I would put collectively a put up to increase on the Topic just a little.

The simplest place to begin earlier than speaking about Change is to take a look at a abnormal MIME message and its related headers. In a MIME message there’s one Message date  header http://instruments.ietf.org/html/rfc4021#section-2.1.1 which outlines “Specifies the date and time at which the creator of the message indicated that the message was full and able to enter the mail supply system” which is a manner of claiming that its the Despatched Time. eg In a Message

Topic: Re: Oh no
Date: Thu, 14 Aug 2014 18:44:52 +1000
Message-ID: <nk0h69wn6waj8s32rnc3kma0.1408005882691@e mail.android.com>

 Because the Message traverses varied MTA’s alongside the best way to its remaining locations, Obtained headers with dates are added to the Transport Headers  of a message indicating the datetime a specific MTA’s processed the message eg

Obtained: from BY2PR03MB459.namprd03.prod.outlook.com (10.141.141.147) by
 DM2PR03MB463.namprd03.prod.outlook.com (10.141.85.20) with Microsoft SMTP
 Server (TLS) id 15.0.859.15 by way of Mailbox Transport; Wed, 29 Jan 2014 22:06:05
 +0000

When the Message lastly arrives at is vacation spot and is delivered by the Retailer to a customers Mailbox two MAPI properties will probably be created to  replicate the Despatched Time and in addition the Date Time the message was delivered by the shop (which ought to match (most up-to-date) obtained header within the message). With POP3 and and a few POP downloaders that is the place the date can get just a little offset and never symbolize the actual time of message supply. However sticking to Change the next two props get set

PR_MESSAGE_DELIVERY_TIME which in EWS can be represented by the Strongly typed DateTimeReceived property

PR_CLIENT_SUBMIT_TIME property which in EWS is represented by the Strongly typed DateTimeSent property.

Change when it shops these dates like with different dates will retailer these in UTC format. When your utilizing Outlook with the default views it can use the PR_MESSAGE_DELIVERY_TIME for each Mail folder different then the Despatched Objects the place the view will use the PR_CLIENT_SUBMIT_TIME.

So everytime you importing EML’s and also you see an surprising Obtained (or Despatched) by date the very first thing to examine is the Transport Headers and have a look at the latest obtained header. In case your lacking these headers then that possibly why your dates aren’t what you count on.

In EWS you’ll be able to entry the Transport Headers by way of the InternetMessageHeaders strongly typed property or you need to use the PR_TRANSPORT_MESSAGE_HEADERS prolonged properties. Relying on the model of Change you might be utilizing there will be points with the strongly typed property so you must learn http://msdn.microsoft.com/EN-US/library/workplace/hh545614(v=exchg.140).aspx . Due to the dimensions of those properties this data will solely be returned once you use a GetItem’s operation

The opposite factor you are able to do with these message dates is observe the period of time it took from undergo the supply of a message eg the next script will use EWS to seize each the PR_MESSAGE_DELIVERY_TIME  and PR_CLIENT_SUBMIT_TIME MAPI properties and use these to calculate the supply time can be parses the Despatched Header datetime and creates a CSV of the output. I’ve put a download of this script right here the code appears like

  1. ## Get the Mailbox to Entry from the 1st commandline argument  
  2.   
  3. $MailboxName = $args[0]  
  4.   
  5. ## Load Managed API dll    
  6.   
  7. ###CHECK FOR EWS MANAGED API, IF PRESENT IMPORT THE HIGHEST VERSION EWS DLL, ELSE EXIT  
  8. $EWSDLL = (($(Get-ItemProperty -ErrorAction SilentlyContinue -Path Registry::$(Get-ChildItem -ErrorAction SilentlyContinue -Path ‘Registry::HKEY_LOCAL_MACHINESOFTWAREMicrosoftExchangeWeb Companies’|Type-Object Title -Descending| Choose-Object -First 1 -ExpandProperty Title)).‘Set up Listing’) + “Microsoft.Exchange.WebServices.dll”)  
  9. if (Take a look at-Path $EWSDLL)  
  10.     {  
  11.     Import-Module $EWSDLL  
  12.     }  
  13. else  
  14.     {  
  15.     “$(get-date -format yyyyMMddHHmmss):”  
  16.     “This script requires the EWS Managed API 1.2 or later.”  
  17.     “Please download and install the current version of the EWS Managed API from”  
  18.     “http://go.microsoft.com/fwlink/?LinkId=255472”  
  19.     “”  
  20.     “Exiting Script.”  
  21.     exit  
  22.     }  
  23.     
  24. ## Set Change Model    
  25. $ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Change2010_SP2    
  26.     
  27. ## Create Change Service Object    
  28. $service = New-Object Microsoft.Change.WebServices.Knowledge.ExchangeService($ExchangeVersion)    
  29.     
  30. ## Set Credentials to use two choices are availible Option1 to use explict credentials or Choice 2 use the Default (logged On) credentials    
  31.     
  32. #Credentials Choice 1 utilizing UPN for the home windows Account    
  33. $psCred = Get-Credential    
  34. $creds = New-Object System.Internet.NetworkCredential($psCred.UserName.ToString(),$psCred.GetNetworkCredential().password.ToString())    
  35. $service.Credentials = $creds        
  36.     
  37. #Credentials Choice 2    
  38. #service.UseDefaultCredentials = $true    
  39.     
  40. ## Select to ignore any SSL Warning points precipitated by Self Signed Certificates    
  41.     
  42. ## Code From http://poshcode.org/624  
  43. ## Create a compilation setting  
  44. $Supplier=New-Object Microsoft.CSharp.CSharpCodeProvider  
  45. $Compiler=$Supplier.CreateCompiler()  
  46. $Params=New-Object System.CodeDom.Compiler.CompilerParameters  
  47. $Params.GenerateExecutable=$False  
  48. $Params.GenerateInMemory=$True  
  49. $Params.IncludeDebugInformation=$False  
  50. $Params.ReferencedAssemblies.Add(“System.DLL”) | Out-Null  
  51.   
  52. $TASource=@ 
  53.   namespace Native.ToolkitExtensions.Internet.CertificatePolicy{ 
  54.     public class TrustAll : System.Internet.ICertificatePolicy { 
  55.       public TrustAll() {  
  56.       } 
  57.       public bool CheckValidationResult(System.Internet.ServicePoint sp, 
  58.         System.Safety.Cryptography.X509Certificates.X509Certificates cert,  
  59.         System.Internet.WebRequest req, int downside) { 
  60.         return true; 
  61.       } 
  62.     } 
  63.   } 
  64. @   
  65. $TAResults=$Supplier.CompileAssemblyFromSource($Params,$TASource)  
  66. $TAAssembly=$TAResults.CompiledAssembly  
  67.   
  68. ## We now create an occasion of the TrustAll and connect it to the ServicePointManager  
  69. $TrustAll=$TAAssembly.CreateInstance(“Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll”)  
  70. [System.Net.ServicePointManager]::CertificatePolicy=$TrustAll  
  71.   
  72. ## finish code from http://poshcode.org/624  
  73.     
  74. ## Set the URL of the CAS (Shopper Entry Server) to use two choices are availbe to use Autodiscover to discover the CAS URL or Hardcode the CAS to use    
  75.     
  76. #CAS URL Choice 1 Autodiscover    
  77. $service.AutodiscoverUrl($MailboxName,{$true})    
  78. “Using CAS Server : “ + $Service.url     
  79.      
  80. #CAS URL Choice 2 Hardcoded    
  81.     
  82. #$uri=[system.URI] “https://casservername/ews/exchange.asmx”    
  83. #$service.Url = $uri      
  84.     
  85. ## Optionally available part for Change Impersonation    
  86.     
  87. #$service.ImpersonatedUserId = new-object Microsoft.Change.WebServices.Knowledge.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)   
  88.   
  89. # Bind to the Inbox Folder  
  90. $folderid= new-object Microsoft.Change.WebServices.Knowledge.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$MailboxName)     
  91. $Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)  
  92.   
  93. $PR_MESSAGE_DELIVERY_TIME = new-object Microsoft.Change.WebServices.Knowledge.ExtendedPropertyDefinition(0x0E06, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::SystemTime)  
  94. $PR_CLIENT_SUBMIT_TIME = new-object Microsoft.Change.WebServices.Knowledge.ExtendedPropertyDefinition(0x0039, [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::SystemTime)  
  95. $PR_TRANSPORT_MESSAGE_HEADERS = new-object Microsoft.Change.WebServices.Knowledge.ExtendedPropertyDefinition(0x007D,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);  
  96. $psPropset= new-object Microsoft.Change.WebServices.Knowledge.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::IdOnly)    
  97. $psPropset.Add($PR_CLIENT_SUBMIT_TIME)  
  98. $psPropset.Add($PR_MESSAGE_DELIVERY_TIME)  
  99. $psPropset.Add($PR_TRANSPORT_MESSAGE_HEADERS)  
  100. $psPropset.Add([Microsoft.Exchange.WebServices.Data.ItemSchema]::Topic)  
  101. #Outline ItemView to retrive simply 1000 Objects      
  102. $ivItemView =  New-Object Microsoft.Change.WebServices.Knowledge.ItemView(1000)    
  103. $ivItemView.PropertySet = $psPropset  
  104. $rptCollection = @()  
  105. $fiItems = $null      
  106. do{      
  107.     $fiItems = $service.FindItems($Inbox.Id,$ivItemView)      
  108.     [Void]$service.LoadPropertiesForItems($fiItems,$psPropset)    
  109.     foreach($Merchandise in $fiItems.Objects){      
  110.         $Headers = $null;  
  111.         $ClientSubmitTime = $null  
  112.         $DeliveryTime = $null  
  113.         [Void]$Merchandise.TryGetProperty($PR_CLIENT_SUBMIT_TIME,[ref]$ClientSubmitTime)  
  114.         [Void]$Merchandise.TryGetProperty($PR_MESSAGE_DELIVERY_TIME,[ref]$DeliveryTime)  
  115.         if($Merchandise.TryGetProperty($PR_TRANSPORT_MESSAGE_HEADERS,[ref]$Headers)){  
  116.             $slen = $Headers.ToLower().IndexOf(“`ndate: “)  
  117.             if($slen -gt 0) choose Topic,HeaderDate,DELIVERY_TIME,SUBMIT_TIME,Diff  
  118.                 $rptobj.Topic = $Merchandise.Topic  
  119.                 $parsedDate = $Headers.Substring(($slen+7),($elen-($slen+7)))                 
  120.                 $rptobj.HeaderDate = [DateTime]::Parse($parsedDate).ToLocalTime()  
  121.                 $rptobj.DELIVERY_TIME = $DeliveryTime.ToLocalTime()  
  122.                 $rptobj.SUBMIT_TIME = $ClientSubmitTime.ToLocalTime()  
  123.                 $rptobj.Diff = [Math]::Spherical($TimeSpan.TotalMinutes,0)  
  124.                 $rptCollection += $rptobj  
  125.               
  126.         }        
  127.     }      
  128.     $ivItemView.Offset += $fiItems.Objects.Depend  
  129.     Write-Host (“Processed “ + $ivItemView.Offset + ” of “ + $fiItems.TotalCount)  
  130. }whereas($fiItems.MoreAvailable -eq $true)   
  131. $rptCollection | Export-Csv -NoTypeInformation -Path “c:Temp$mailboxName-mTimes.csv”  
  132. Write-Host(“Exported to c:Temp$mailboxName-mTimes.csv”)  

Leave a Reply