May 21, 2010

Send an Email

PowerShell v2 has what seems to still be a relativley unknown cmdlet that allows you to send email. Here is an example of how to use it:

Send-MailMessage -To user@contoso.com `
				 -From user@fabrikam.com `
				 -Subject "Test" `
				 -Body "Test" `
				 -SmtpServer smtp.domain.com

On the otherhand, if you are still using v1, you can use the good ole System.Net.Mail classes:

function Send-Email {
	param($to, $from, $subject, $body, $smtpServer)
	
	$msg = new-object Net.Mail.MailMessage
	$smtp = new-object Net.Mail.SmtpClient($smtpServer)
	
	$msg.From = $from
	$msg.To.Add($to)
	$msg.Subject = $subject
	$msg.Body = $body
	
	$smtp.Send($msg)	
}
  1. dowding reblogged this from powershell
  2. powershell posted this