我可以将TLS与Send-MailMessage cmdlet一起使用吗?

时间:2016-10-10 21:55:49

标签: powershell

我正在尝试使用PowerShell发送电子邮件,但需要使用TLS。我可以使用Send-MailMessage cmdlet以任何方式执行此操作吗?

这是我的代码:

$file = "c:\Mail-content.txt"

if (test-path $file)
{

    $from = "afgarciact@gmail.com"
    $to = "<slopez@comfama.com.co>","<carloscu@comfama.com.co>"
    $pc = get-content env:computername
    $subject = "Test message " + $pc
    $smtpserver ="172.16.201.55"
    $body = Get-Content $file | Out-String


[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true }

     foreach ($recipient in $to)
    {
        write-host "Sent to $to"
        Send-MailMessage -smtpServer $smtpserver -from $from -to $recipient -subject $subject  -bodyAsHtml $body -Encoding ([System.Text.Encoding]::UTF8)
    }



}
else
{
write-host "Configuración"
}

非常感谢!

1 个答案:

答案 0 :(得分:4)

确保您指定-UseSsl开关:

Send-MailMessage -SmtpServer $smtpserver -UseSsl -From $from -To $recipient -Subject $subject -BodyAsHtml $body -Encoding ([System.Text.Encoding]::UTF8)

如果SMTP服务器使用特定端口进行SMTP over TLS,请使用-Port参数:

Send-MailMessage -SmtpServer $smtpserver -Port 465 -UseSsl -From $from -To $recipient -Subject $subject -BodyAsHtml $body -Encoding ([System.Text.Encoding]::UTF8)

如果要确保始终协商TLS(而不是SSL 3.0),请在SecurityProtocol类上设置ServicePointManager属性:

[System.Net.ServicePointManager]::SecurityProtocol = 'Tls,TLS11,TLS12'