通过 Powershell 2.0 发送电子邮件

Send email via Powershell 2.0

如何使用 SMTP 服务器的 IP 地址和端口号 发送 Powershell (v 2.0) 脚本的结果(例如 xxx.xxx.xx.xxx:xx)匿名身份验证 不需要安全连接?

这里是一个小脚本,别忘了,你在.NET框架的顶端:

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)