通过Powershell 2.0发送电子邮件

时间:2016-11-07 11:53:17

标签: powershell smtp powershell-v2.0

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

1 个答案:

答案 0 :(得分:2)

这是一个小脚本,请不要忘记,您处于.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)
相关问题