0

Send email with Powershell

Sending email with Windows Powershell is a relatively easy thing to do. PowerShell provides the user with the Net.Mail.SmtpClient class which contains the Send fucntion. If you want to send an email from PowerShell you can do the following:

# Create the SMTP client object and initialise.
	$SMTPClient = New-Object Net.Mail.SmtpClient("mail@example.com", 25)
	$SMTPClient.EnableSsl = true
	$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(
		"username", "password");

	# Try to send the message.
	try 
	{
		$SMTPClient.Send("mail@example.com", "mail@example.com", "subject", "body")
		"Message sent successfully."
	}
	# Catch any error that occurs.
	catch
	{
		"Exception caught in SendEmail(): " + $_.Exception.ToString()
	}

Jason Edwards