gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:57:16 26 4
gpt4 key购买 nike

我正在尝试使用 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"
}

非常感谢!

最佳答案

确保您指定 -UseSsl转变:

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

如果 SMTP 服务器使用特定端口用于基于 TLS 的 SMTP,请使用 -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 上的属性(property)类(class):
[System.Net.ServicePointManager]::SecurityProtocol = 'Tls,TLS11,TLS12'

关于powershell - 我可以将 TLS 与 Send-MailMessage cmdlet 一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39967359/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com