Stackoverflowuser
Stackoverflowuser

Reputation: 243

Send email via Powershell 2.0

How can I send the result of a Powershell (v 2.0) script by using IP address and port number of an SMTP server (e.g xxx.xxx.xx.xxx:xx) with anonymous authentication which does not require secure connection?

Upvotes: 1

Views: 2071

Answers (1)

JPBlanc
JPBlanc

Reputation: 72630

Here is a small script, don't forget, you are on the top of the .NET framework:

Param([string[]] $to = $(throw "Please Specify a destination !"),
      [string] $subject = "<No Subject>",
      [string] $body = $(throw "Pleasr specify a content !"),
      [string] $smtpHost = $(throw "Please specify a SMTP server !"),
      [string] $from = $(throw "Please specify from email address !")
)

## Message creation
$email = New-Object System.Net.Mail.MailMessage

## Fill the fields
foreach($mailTo in $to)
{
  $email.To.Add($mailTo)
}

$email.From = $from
$email.Subject = $subject
$email.Body = $body

## Send the message
$client = New-Object System.Net.Mail.SmtpClient $smtpHost
$client.UseDefaultCredentials = $true
$client.Send($email)

Upvotes: 3

Related Questions