When you want to test mail flow through PowerShell using Basic authentication (username and password), it’s important to do this in a safe, reliable, and reproducible way. In this blog, I’ll show you how to send a test email using basic authentication while retrieving the username and password neatly from an external file. This helps you work in a structured manner, avoid mistakes, and keep your scripts clean. Because testing via PowerShell is often part of troubleshooting, monitoring, or validation, it pays to set up this process properly. Below, you’ll find a step‑by‑step explanation of how to approach this. For testing mailflow via PowerShell we use the SMTP server of Azure Communication Services. You can find more information about Azure Communication Services here.
Why testing mailflow via PowerShell is so effective
- You can quickly validate whether SMTP settings are correct
- You automate test flows within CI/CD or monitoring
- You work consistently, without manual input
- You maintain control over authentication and security
By automating this testing through PowerShell, you create a repeatable test that you can run at any moment.
Storing credentials securely
Instead of hardcoding the username and password in your script, it’s better to store them in a set of files:
1. Files for the username, password, and email addresses
For example:
C:\Temp\smtp-user.txt
C:\Temp\email-to.txt
C:\Temp\email-from.txt
C:\Temp\email-pass.txt
2. File for the password (SecureString)
Create the file like this:
# Securely request the password
$sec = Read-Host "Enter SMTP password" -AsSecureString
# Store it encrypted
$sec | ConvertFrom-SecureString | Out-File "C:\Temp\smtp-pass.txt"
This ensures the password is stored in encrypted form. You may also want to check out the blog about Credentials in PowerShell.
Sript for testing mailflow via PowerShell
The script below is fully based on retrieving the credentials from files first.
Paths to files
$userFile = "C:\Temp\smtp-user.txt"
$emailFile_To = "C:\Temp\email-to.txt"
$passFile = "C:\Temp\smtp-pass.txt"
$emailFile_From = "C:\Temp\email-from.txt"
Retrieve username and password
$user = Get-Content $userFile
$to = Get-Content $emailFile_To
$from = Get-Content $emailFile_From
$sec = Get-Content $passFile | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential($user, $sec)
Send test email
# Paths to files
$userFile = "C:\Temp\smtp-user.txt"
$emailFile_To = "C:\Temp\email-to.txt"
$passFile = "C:\Temp\smtp-pass.txt"
$emailFile_From = "C:\Temp\email-from.txt"
# Retrieve username and password
$user = Get-Content $userFile
$to = Get-Content $emailFile_To
$from = Get-Content $emailFile_From
$sec = Get-Content $passFile | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential($user, $sec)
# Send test email
Send-MailMessage `
-From $from `
-To $to `
-Subject "Mail testen via PowerShell" `
-Body "Dit is een test via PowerShell" `
-SmtpServer "smtp.azurecomm.net" `
-Port 587 `
-UseSsl `
-Credential $cred
Why this approach is better for testing mailflow via PowerShell
- You follow best practices for automation
- You prevent passwords from ending up in scripts
- You can easily rotate credentials without modifying code
- Your script remains clean, organized, and secure
Conclusion: the best way to test mail flow via PowerShell
In short, testing mail flow via PowerShell becomes much safer and more professional when you retrieve credentials from files. By applying this method, you create a robust workflow ready for production, monitoring, and troubleshooting. It’s a small investment that delivers significant stability.