Search code examples
powershellemailexchangewebservices

EWS Mark mail as read after moving


With the help of Exchange Web Services we're trying to move a mail to a specific folder and then mark it as read. To do this we use the following code:

Write-Verbose "Move mail to '$('\PowerShell\' + $SQLTickets.ScriptName)'"
$DestinationPathID = Find-MailboxFolderIDHC @FindMailParams -Path ('\PowerShell\' + $SQLTickets.ScriptName)
$Mail.Move($DestinationPathID) | Out-Null

Write-Verbose 'Mark mails as read'
$Mail.IsRead = $true
$Mail.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AutoResolve)

When I check the result of this action the mail is indeed correctly moved to the folder en has the IsRead property set to True. However, this is not visible in MS Outlook, it's still not marked as Read.

When it hits this line:

$Mail.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AutoResolve)

It throws the follwing error which I can't seem to fix:

Exception calling "Update" with "1" argument(s): "The specified object was not found in the store."

Solution

  • Mark it read first, then move it like this:

    $Mail.IsRead = $true
    $Mail.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AutoResolve)
    [VOID]$Mail.Move($DestinationPathID)