Mailosaur logo
Mailosaur logo

How to test email and SMS with PowerShell

Learn how to test email and SMS messages in PowerShell, using Mailosaur.

What is PowerShell?
A hashtag icon

PowerShell is a cross-platform task automation solution from Microsoft, made up of a command-line shell, a scripting language, and a configuration management framework. PowerShell runs on Windows, Linux, and macOS. It can be used for any kind of QA test automation.

As you are reading an article about testing email and SMS in PowerShell, you probably already know how to use it, however if you are new to PowerShell, we would recommend reading the official product documentation for PowerShell to get started.

What is Mailosaur?
A hashtag icon

Mailosaur is a service that allows you to capture and test email and SMS messages. This is crucial for products and websites that send out emails (such as password resets, account setup, marketing emails, etc.) or SMS messages (such as multi-factor authentication, verification, etc.)

With Mailosaur you get an unlimited supply of test email addresses, plus virtual SMTP servers, test SMS numbers to work with, and more - all accessible within your PowerShell scripts.

Get Started
A hashtag icon

Install and load the Mailosaur NuGet package
A hashtag icon

Install the latest version of the Mailosaur package from nuget:

install-package Mailosaur -source

Next, load the assembly into PowerShell:

$pkgZip = [System.IO.Compression.ZipFile]::Open((Get-Package Mailosaur).Source,"Read")
$pkgStream = [System.IO.MemoryStream]::new()
$pkgReader = [System.IO.StreamReader]($pkgZip.entries[2]).Open()
[byte[]]$pkgBytes = $pkgStream.ToArray()

Instantiate the Mailosaur client
A hashtag icon

You are now ready to start using the package. The value for YOUR_API_KEY is covered in the next step (creating an account):

$mailosaur = [Mailosaur.MailosaurClient]::new("YOUR_API_KEY")

API Reference
A hashtag icon

The .NET library is powered by the Mailosaur email & SMS testing API. You can easily check out the API itself by looking at our API reference documentation or via our Postman or Insomnia collections:

Run in PostmanRun in Insomnia

1. Create an account
A hashtag icon

Create a free trial account for Mailosaur, via the website.

Once you have this, navigate to the API tab to find the following values:

  • Server ID - Servers act like projects, which group your tests together. You need this ID whenever you interact with a server via the API.
  • Server Domain - Every server has its own domain name. You’ll need this to send email to your server.
  • API Key - You can create an API key per server (recommended), or an account-level API key to use across your whole account. Learn more about API keys.

2. Test email addresses with Mailosaur
A hashtag icon

Mailosaur gives you an unlimited number of test email addresses - with no setup or code required!

Here’s how it works:

  • When you create an account, you are given a server.
  • Every server has its own Server Domain name (e.g.
  • Any email address that ends with @{YOUR_SERVER_DOMAIN} will work with Mailosaur without any special setup. For example:
  • You can create more servers when you need them. Each one will have its own domain name.

Can’t use test email addresses? You can also use SMTP to test email. By connecting your product or website to Mailosaur via SMTP, Mailosaur will catch all email your application sends, regardless of the email address.

3. Find an email
A hashtag icon

Now that you have a sample project, your server’s credentials, and an email to test, then you’re ready to continue.

Open the file in your new project called Program.cs and type or paste in this code sample, replacing the template code with the values from step 3 above:

# Available in the API tab of a server
$apiKey = "YOUR_API_KEY"
$serverId = "SERVER_ID"
$serverDomain = "SERVER_DOMAIN"

$mailosaur = [Mailosaur.MailosaurClient]::new($apiKey)

# Wait for an email to arrive, matching this search criteria
$criteria = [Mailosaur.Models.SearchCriteria]::new()
$criteria.SentTo = "anything@" + $serverDomain
$message = $mailosaur.Messages.Get($serverId, $criteria);

Find an SMS message
A hashtag icon

Important: Trial accounts do not automatically have SMS access. Please contact our support team to enable a trial of SMS functionality.

If your account has SMS testing enabled, you can reserve phone numbers to test with, then use the Mailosaur API in a very similar way to when testing email:

# ...

$criteria = [Mailosaur.Models.SearchCriteria]::new()
$criteria.SentTo = "4471235554444"
$mailosaur.Messages.Get($serverId, $criteria);

Testing plain text content
A hashtag icon

Most emails, and all SMS messages, should have a plain text body. Mailosaur exposes this content via the Text.Body property on an email or SMS message:

$message.Text.Body # "Hello world."

Testing HTML content
A hashtag icon

Most emails also have an HTML body, as well as the plain text content. You can access HTML content in a very similar way to plain text:

$message.Html.Body # "<html><head ..."

When an email is sent with an HTML body, Mailosaur automatically extracts any hyperlinks found within anchor (<a>) and area (<area>) elements and makes these available via the Html.Links array.

Each link has a text property, representing the display text of the hyperlink within the body, and an href property containing the target URL:

$message.Html.Links.Count # 2

# Accessing the HTML link content
$firstLink = $message.Html.Links[0]
$firstLink.Text # "Google Search"
$firstLink.Href # ""

Important: To ensure you always have valid emails. Mailosaur only extracts links that have been correctly marked up with <a> or <area> tags.

Mailosaur auto-detects links in plain text content too, which is especially useful for SMS testing:

$message.Text.Links.Count # 1

# Accessing the HTML link content
$firstLink = $message.Text.Links[0]
$firstLink.Href # ""

Working with attachments
A hashtag icon

If your email includes attachments, you can access these via the Attachments property:

# How many attachments?
$message.Attachments # 2

Each attachment contains metadata on the file name and content type:

$firstAttachment = $message.Attachments[0]
$firstAttachment.FileName # "contract.pdf"
$firstAttachment.ContentType # "application/pdf"

The Length property returns the size of the attached file (in bytes):

$firstAttachment = $message.Attachments[0]
$firstAttachment.Length # 4028

Working with images and web beacons
A hashtag icon

The Html.Images property of a message contains an array of images found within the HTML content of an email. The length of this array corresponds to the number of images found within an email:

# How many images in the email?
$message.Html.Images.Count # 1

Remotely-hosted images
A hashtag icon

Emails will often contain many images that are hosted elsewhere, such as on your website or product. It is recommended to check that these images are accessible by your recipients.

All images should have an alternative text description, which can be checked using the Alt attribute.

$image = $message.Html.Images[0]
$image.Alt # "Hot air balloon"

Triggering web beacons
A hashtag icon

A web beacon is a small image that can be used to track whether an email has been opened by a recipient.

Because a web beacon is simply another form of remotely-hosted image, you can use the Src attribute to make an HTTP request to that address using Invoke-WebRequest:

$image = $message.Html.Images[0]
$image.Src # ""

# Make an HTTP call to trigger the web beacon
Invoke-WebRequest -URI $image.Src

Spam checking
A hashtag icon

You can perform a SpamAssassin check against an email. The structure returned matches the spam test object:

$result = $mailosaur.Analysis.Spam($message.Id)

$result.Score # 0.5

foreach($r in $result.SpamFilterResults.SpamAssassin) {