Friday, January 18, 2013

Send email PowerShell

Create a script like the following and save it as send_email.ps1.

$from = New-Object System.Net.Mail.MailAddress "from@domain.com" 

$to = New-Object System.Net.Mail.MailAddress "jdoe@gmail.com" 

$messge = new-object system.Net.Mail.MailMessage $from, $to

$messge.IsBodyHTML = $true
$messge.Subject = "Test email to jdoe"
$messge.body = "Lorem ipsum dolor sit amet. Regards."

$smtpServer = 'localhost'
$port = '25'

$smtp = new-object Net.Mail.SmtpClient($smtpServer, $port)

$smtp.Send($messge);

Open PowerShell, go to the directory where the script was saved and execute it.

If you have permissions to execute scripts in Powershell, and the SMTP server is correctly configured, and the recipient email account exists, then you should get the email.

No comments: