Sending Emails Securely (via SSL)

by Jan 24, 2012

In a previous tip we showed how to use the Send-MailMessage cmdlet to send off emails and preserve special characters by using UTF8 encoding. When you try this with your freemailer, you may run into issues where your SMTP server complains about needing a secure connection.

So, when you receive such an error message, make sure you add the switch parameter -UseSsl. This only works right though if you use PowerShell v3 (the public CTP2 is readily available). In PowerShell v2, Send-MailMessage does not use the correct port for SSL connections.

Here is a sample that uses the live.com freemailer (formally called "Hotmail") which does require a secure connection:

PS> Send-MailMessage -Body 'My mail message can contain special characters: äöüß' -From youraccountname@hotmail.com -to someone@somewhere.com -Credential youraccountname@hotmail.com -SmtpServer smtp.live.com -Subject 'Sending Mail from PowerShell' -Encoding UTF8 -UseSsl

Note that PowerShell v3 Send-MailMessage accepts "UTF8" directly so you no longer need to submit the awkward ([System.Text.Encoding]::UTF8).

There are plenty of websites that list SMTP server URLs for your favorite provider: http://www.e-eeasy.com/SMTPServerList.aspx.

Twitter This Tip!
ReTweet this Tip!