Following on from my last post about errors when running Test-ServiceHealth for Exchange 2013 Client Access servers, here is another PowerShell test cmdlet that behaves unexpectedly in Exchange 2013.

Test-Mailflow, as the name suggests, is used to test mail flow for Mailbox servers. The TechNet page provides the following example syntax:

Test-Mailflow Mailbox1 -TargetMailboxServer Mailbox2

What isn’t documented is the error that occurs if you try to run that command when your PowerShell session is connected to a server other than “Mailbox1”.

[PS] C:scripts>Test-Mailflow E15MB1 -TargetMailboxServer E15MB2

[Microsoft.Mapi.MapiExceptionSendAsDenied]: MapiExceptionSendAsDenied:
Unable to submit message. (hr=0x80070005,ec=1244)

This error did not occur in Exchange 2010 under the same conditions.

As with the Test-ServiceHealth issues from my last post this situation is frustrating for me because I use Test-Mailflow in my Test-ExchangeServerHealth.ps1 script. So I needed to find a way to still perform the test while avoiding the error.

The solution was found by using PowerShell remoting. I’ve written this proof of concept code to demonstrate how remoting can be used to get a result for Test-Mailflow.

Note this is demo code only and is not intended to be a functional script. I will be incorporating this code into Test-ExchangeServerHealth.ps1 in the near future.

#Proof of concept code to test mail
#flow on remote E15 servers

param (
	[Parameter( Mandatory=$false)]

Write-Verbose "Creating PSSession for $server"
    $url = (Get-PowerShellVirtualDirectory -Server $server | Where {$_.Name -eq "Powershell (Default Web Site)"}).InternalURL.AbsoluteUri
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $url -ErrorAction STOP
    Write-Verbose "Something went wrong"
    Write-Warning $_.Exception.Message

    Write-Verbose "Running mail flow test on $Server"
    $result = Invoke-Command -Session $session {Test-Mailflow} -ErrorAction STOP
    $testresult = $result.TestMailflowResult

    Write-Verbose "An error occurred"
    Write-Warning $_.Exception.Message

Write-Host "Mail flow test: $testresult"

Write-Verbose "Removing PSSession"

Remove-PSSession $session.Id

Here is an example of the output.

[PS] C:scripts>.Test-E15MailFlow.ps1 E15MB1
Mail flow test: Success

[PS] C:scripts>.Test-E15MailFlow.ps1 E15MB2 -Verbose
VERBOSE: Creating PSSession for E15MB2
VERBOSE: Running mail flow test on E15MB2
Mail flow test: Success
VERBOSE: Removing PSSession

About the Author

Paul Cunningham

Paul is a former Microsoft MVP for Office Apps and Services. He works as a consultant, writer, and trainer specializing in Office 365 and Exchange Server. Paul no longer writes for


  1. Jørgen Kaare Jensen

    See the suggested code did never end up in the script – is there a plan to update the script?

  2. Andrew

    Having this issue on some of my Exchange 2016 servers. Any ideas? Running the script locally on the Exchange servers works fine.

  3. Andy B

    Hi Paul

    Did the code ever make it into the script? I’m having the same issues with the latest version.

    Thanks for your work by the way – it’s a massive help!



  4. Patrick Butler

    Intresting, so using this information I was able to configure mailflow in the Test-ExchangeServerHealth.ps1 by adding a variable $lserver = hostname and then in the mailflow adding/modifying
    $flow = Test-Mailflow $lserver -TargetMailboxServer $server -ErrorAction Stop

    1. Tashfin Ikram

      Thank you for the code. This worked perfectly!

Leave a Reply