This is a small script which you can use to send E-Mail from Windows Powershell. This script has an hardcoded password variable because this solution is mostly used for automation. But you could also use the get-credentials command to get the credentials from a user.
# Configuration $emailFrom = "[email protected]" $emailTo = "[email protected]yourdomain.com" $emailSubject = "my subject" $emailMessage = "my message" $smtpServer = "mail.server.com" $smtpUserName = "username" # This could be also in e-mail address format $smtpPassword = "password" $smtpDomain = "" # SMTP Object $smtp = New-Object System.Net.Mail.SmtpClient($smtpServer) $mailCredentials = New-Object System.Net.NetworkCredential $mailCredentials.Domain = $smtpDomain $mailCredentials.UserName = $smtpUserName $mailCredentials.Password = $smtpPassword $smtp.Credentials = $mailCredentials # Send E-Mail $smtp.Send($emailFrom, $emailTo, $emailSubject, $emailMessage)
Update:
Thanks to Jeffery Hicks for correcting me, since Powershell v2 there is a much easier way to send a Mail with the Send-MailMessage cmdlet. http://go.microsoft.com/fwlink/?LinkID=135256
Tags: Credentials, email, Mail, Microsoft, PowerShell, Send Mail, SMTP, Windows, Windows Powershell Last modified: September 7, 2011
Why not simply use the Send-MailMessage cmdlet? Your suggestion is what we needed for PowerShell v1 but v2 makes this so much easiier with an actual cmdlet.
Here’s the online help link: http://go.microsoft.com/fwlink/?LinkID=135256
Yep, agree with Jeff. Send-MailMessage does this job. BTW, thanks for your post.
Thanks for the fix for version1. I have that on some old servers and needed it. Googled all over and yours was the only one that worked!!
its possible attach file with this script?
Hi, thank for a post!
What about SSL connection? SMTP server requires