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]" $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)
Tags: Credentials, email, Mail, Microsoft, PowerShell, Send Mail, SMTP, Windows, Windows Powershell Last modified: September 7, 2011