Test sending e-mails from mailserver using PowerShell

I’ve been using Telnet for a while now to test sending emails from a SMTP relay server.
But if you are using a Windows server/client, the easier way is to use Powershell, as Telnet is unforgiving for syntax errors.

If you are directly logged in to the SMTP server, you can use this PowerShell code:

Send-MailMessage -SMTPServer localhost -To xxxx@gmail.com -From xxxx@domain.com -Subject "Test Email" -Body "Hi, this is a test email sent via PowerShell."

If you, for example, want to test sending mails relaying through Office 365, you first need to enter your credentials which you want to connect with:

$msolcred = get-credential

After you entered your credentials, use this PowerShell code (where xxx@domain.com is the same email address as the one entered in the previous step):

Send-MailMessage -SMTPServer smtp.office365.com -Credential $msolcred -UseSsl -Port 587 -From xxx@domain.com -To xxxx@gmail.com -Subject “Test
Email” -Body “Hi, this is a test email sent via PowerShell.”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.