Send email with Powershell

There’s two methods I often use for sending email in my Powershell scripts. The first uses the Send-MailMessage cmdlet and the second resorts to using the .Net Framework. Why do I use two methods? Well….

Send-MailMessage -To '' -From '' -Subject 'Test Message' -Body 'This is a test message.' -SmtpServer '';

The Send-MailMessage cmdlet is powerful and easy to use but it’s lacking one important switch. If you’re using a smtp server on a port other than the default (25) then you cannot use this cmdlet to send emails. There’s no switch available to change the port used to send email. Here’s how I get around it.

$smtpClient = New-Object System.Net.Mail.SmtpClient;
$smtpClient.Host = '';
$smtpClient.Port = 8025;
$smtpClient.Send('','','Test Message', 'This is a test message.');

Powershell’s ability to hook into the .Net framework allows us to get around any limitation existing in the built in cmdlets. However, I’m still rooting for a –Port switch in the next version of Powershell!

Leave a Reply