SMTP load testing is crucial for any organization/engineer to understand an email servers performance. SMTP load testing will help you understand how a particular server with perform with a large SMTP inbound load.

It’s pretty easy to send emails from a Powershell script by utilizing an SMTP server and this article will show you how to do that. We’ll start by looking at how we can securely store an SMTP password and utilize it in a looping mass email script. Feel free to use the script for your own testing and modification.

 

Create an encrypted password file

In Powershell, navigate to the directory where you will be storing the encrypted password file and script. Simply paste the following command in a Powershell screen. You won’t see it prompt for a password or anything but you will see a blinking cursor on the newline below the command you just typed. Type in the password to your email account. It will create an encrypted password file.

Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File -FilePath username@domain.net.securestrin
Encrypted password file

The Script (For Email SMTP Load testing)

Once we have the password formatted in the .securestring file you can modify the script below. Rename the encrypted password file variable, modify the SMTP IP and port, modify the email to and from addresses and modify the script settings to indicate the total number of emails to be sent and at what interval those emails should be sent. You can even add attachments.

#Techlikethis.com SMTP Mass Email Script
 $PSEmailServer = "192.168.50.2"
 $SMTPPort = 25
 $SMTPUsername = "domain\user"
 $EncryptedPasswordFile = "username@domain.net.securestring "
 $SecureStringPassword = Get-Content -Path $EncryptedPasswordFile | ConvertTo-SecureString
 $EmailCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $SMTPUsername,$SecureStringPassword
 $MailTo = "email1@domain.com"
 $MailFrom = "email2@domain.com"
 $scriptSettings = @{
   NumberOfEmails = 400
   TimeBetweenEmails = 1 #in seconds
 }
 for ($i=1; $i -le $scriptSettings.NumberOfEmails; $i++) {
   try { 
     Send-MailMessage -From $MailFrom -To $MailTo -Subject "Email number $i" -Body "Email number $i" -Port $SMTPPort -Attachments .\bg.zip -Credential $EmailCredential
   } catch { 
     "Error sending email $i" 
   }
   Start-Sleep -Seconds $scriptSettings.TimeBetweenEmails
 }