Using PowerShell to generate a flow of email messages to ease diagnosing message flow (“Send-MailMessages.ps1”)

Posted: 26/08/2010 in Exchange, PowerShell
Tags: ,

I find myself again and again needing a constant flow of email messages to ease diagnosing message flow, so I wrote this little script to assist me. I mainly use this scripted function to test load balancing and perform latency checks but it has other benefits. One such recent example was where I needed to generating enough smtp traffic for a good sized network capture to diagnose a comms issue, this was easily achieved with the AttachmentSizeMB parameter.

I have also added comment-based help with a couple of examples, here’s the code:

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin -ErrorAction SilentlyContinue
function Send-MailMessages {

Generate a constant flow of messages for diagnostic purposes.

This script is designed to assist in generating email messages for testing external message flow to and from your messaging infrastructure.
The ability to quickly send a batch of messages with an attachment on a schedule can help track flow issues or to simply be used to confirm mail routing.

Send-MailMessages -To -From -MessageCount 10 -SecondsDelay 10 -AttachmentSizeMB 1

Send 10 emails to every 10 seconds with a 1MB Attachment

Send-MailMessages -MessageCount 48 -SecondsDelay 1800

Send an email every 30 minutes for 24 hours.


File Name: Send-MailMessages.ps1
Author: jfrmilner
Requires: Powershell V2
Requires: Exchange Managemnent Shell (Only used to auto find the smtpServer)
Legal: This script is provided "AS IS" with no warranties or guarantees, and confers no rights. You may use, modify, reproduce, and distribute this script file in any way provided that you agree to give the original author credit.
Version: v1.0 - 2010 Aug 08 - First Version*
Version: v1.1 - 2012 April 26 - Fixed when only a single HT Server is available. Added check for existing file. Fixed attachment parameter to use varible.


param ( [Parameter(Mandatory=$false)] $To = "", [Parameter(Mandatory=$false)] $From = "", $AttachmentSizeMB=$null, $MessageCount=2, $SecondsDelay=10 )

$messageParameters = @{
 Body = $null | ConvertTo-Html -Body "<H2> Test Message, Please ignore </H2>" | Out-String
 From = $from
 To = $to
 SmtpServer = @(Get-TransportServer)[0].Name.ToString()
if ($AttachmentSizeMB) {

if ((Test-Path $Env:TMP\$($AttachmentSizeMB)mbfile.txt) -ne $true) {
 fsutil file createnew $Env:TMP\$($AttachmentSizeMB)mbfile.txt $($AttachmentSizeMB * 1MB)
$messageParameters.Add("attachment", "$Env:TMP\$($AttachmentSizeMB)mbfile.txt") }

1..$MessageCount | % { sleep -Seconds $secondsDelay ; Send-MailMessage @messageParameters -Subject ("Mailflow Test Email - " + (Get-Date).ToLongTimeString() + " Message " + $_ + " / $MessageCount") -BodyAsHtml }


This is an example of the what you can expect to see in the recipients mailbox:



  1. You have a line in your splat: “Body = $null | ConvertTo-Html -Body ” Test Message, Please ignore ” | Out-String”

    Have you noticed any difference between doing it this way versus:

    Body = “blah”
    BodyAsHtml = $true

    I’d be interested to know…

  2. jfrmilner says:

    I also used the BodyAsHtml switch but I believe this line was from an earlier version where I used Net.Mail.SmtpClient before the Send-MailMessage cmdlet was available, or it might have been due to the H2 formatting.

  3. Charles says:

    I just tried this code by changing my credentials To and from
    But i have not received any emails

    • jfrmilner says:


      Check your Transport logs to confirm your message flow. Try using the Send-MailMessage on its own and see if you can work out what could be the issue, the script is basically a loop wrapper around this cmdlet.

  4. […] an example, here is a script that John Milner wrote and posted on one of his blog’s article (thank you very much John ! – Just comment my article if you want me to remove your script from […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s