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

Ser*_*ron 3 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"
}
Run Code Online (Sandbox Code Playgroud)

非常感谢!

Mat*_*sen 7

确保指定-UseSsl开关:

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

如果SMTP服务器将特定端口用于TLS上的SMTP,请使用-Port参数:

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

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

[System.Net.ServicePointManager]::SecurityProtocol = 'Tls,TLS11,TLS12'
Run Code Online (Sandbox Code Playgroud)