In Windows there is no way to natively send mail from the Command Prompt, but because PowerShell allows you to use the underlying .Net Framework, you can easily create and send an e-mail from the command line.
Note: I have uploaded a sample over here, due to many requests.
Sending Email From PowerShell
Note: We decided to use the GMail SMTP Servers for this article, that means you will need a GMail account to send mail using the provided code. However, you could easily hack my script to work with any SMTP Server should you want to.
The first thing you need to do is fire up PowerShell.
It’s pretty easy to send an e-mail from PowerShell, all you need to do is copy the template we provided and change some of the details.
$EmailFrom = “firstname.lastname@example.org”
$EmailTo = “email@example.com”
$Subject = “The subject of your email”
$Body = “What do you want your email to say”
$SMTPServer = “smtp.gmail.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“usr”, “pass”);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
You will need to change the following:
- $EmailFrom = Your GMail address.
- $EmailTo = The recipient’s email address.
- $Subject = What you want the subject of the mail to say.
- $Body = What you want the main part of the mail to say.
- usr = You will need to replace this with your GMail username.
- pass = You will need to replace this with your GMail password.
Below is an example of me sending mail to myself.
Note: For obvious reasons, I removed GMail credentials from the screenshot.
That’s all there is to it.
Taylor Gibb is a Microsoft MVP and all round geek, he loves everything from Windows 8 to Windows Server 2012 and even C# and PowerShell. You can also follow him on Google+
- Published 07/30/12