Send Bulk Test Emails with PowerShell

I’m doing some load testing on one of my SMTP Smart Hosts, and needed to send a few thousand emails in a controlled space of time. I’ve written this quick little script to do so:

$scriptSettings = @{
NumberOfEmails = 100
TimeBetweenEmails = 1 #in seconds
MailDestination = ‘user.name@contoso.com’
MailSubject = ‘Test’
MailServer = ‘mail.contoso.com’
MailFrom = ‘no-reply@contoso.com’

}

for ($i=1; $i -le $scriptSettings.NumberOfEmails; $i++) {
try { Send-MailMessage `
-To $scriptSettings.mailDestination `
-Subject $scriptSettings.mailSubject `
-SmtpServer $scriptSettings.mailServer `
-From $scriptSettings.From `
-Body “Email number $i”

} catch { “Error sending email $i” }

Start-Sleep -Seconds $scriptSettings.TimeBetweenEmails
}