Quickly Generating Test E-mails

I needed to mock-up a full Outlook inbox for a document I’m writing, and of course I figured there’d have to be a way to do this using PowerShell.

Turns out it’s really simple. Here’s the quick and dirty script I lashed up that will dump as many messages as you like into a given mailbox, with a randomly-selected sender, subject and mail domain.

I ran this script on my Windows 8.1 PC, targeting another mail user on the local system.

Run it from the PowerShell command line and simply tell it how many messages you want to send.

Note that in this script I’ve hard-coded the recipient and the mail server, but you can of course build on this script to pass it from the command line. If Exchange rejects you, it might be a permissions issue (anonymous / mail relay).

param(
	[alias("how")][int]$HowMany
)

#Just populate these lists with as many dummy values as you like:
$firstname = @("Homer", "Marge", "Bart", "Maggie", "Brad", "Georgie")
$lastname = @("Simpson", "Pitt", "Parker", "Mulder", "Scully")
$subject = @("FW: Meeting request", "Lunch?", "Testing schedule", "E-mail test messages", "Random text", "More unnecessary inbox clutter", "Linkedin news")
$domain = @("contoso", "fabrikam", "spam-haus", "m1crosoft")

for ($i = 1; $i -le $howmany; $i++)
{
	#Get-Random will only ever issue a number ONE LESS than "-maximum", hence not needing to account for the zero offset:
	$first = $firstname[(get-random -minimum 0 -maximum ($firstname.Count))]
	$last = $lastname[(get-random -minimum 0 -maximum ($lastname.Count))]
	$subj = $subject[(get-random -minimum 0 -maximum ($subject.Count))]
	$dom = $domain[(get-random -minimum 0 -maximum ($domain.Count))]

	Send-MailMessage -from "$($First) $($last) <$($first).$($last)@$($dom).com>" -to "<Recipient>@<MailDomain>" -subject "$($Subj)" -body "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." -SmtpServer <YourHubTransportServer'sFQDN>
}

commandline

But of course if you only want to send a quickie, perhaps with an attachment, try this:

Send-MailMessage -from "voicemail@contoso.com" -to "<recipient>@<maildomain>" -subject "Voicemail (0:24)" -body "A new voicemail message is attached." -SmtpServer <YourHubTransportServer'sFQDN> -attachment "I:\voicemail.wav"

 
-G.

Leave a Reply

Your email address will not be published. Required fields are marked *

... and please just confirm for me that you're not a bot first: Time limit is exhausted. Please reload the CAPTCHA.