我可以将 TLS 与 Send-MailMessage cmdlet 一起使用吗? [英] Can I use TLS with Send-MailMessage cmdlet?
本文介绍了我可以将 TLS 与 Send-MailMessage cmdlet 一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 PowerShell 发送电子邮件,但需要使用 TLS.有什么办法可以使用 Send-MailMessage cmdlet 做到这一点?
I am trying to send an email using PowerShell, but need to use TLS. Any way I can do that using 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
开关:
Make sure your specify the -UseSsl
switch:
Send-MailMessage -SmtpServer $smtpserver -UseSsl -From $from -To $recipient -Subject $subject -BodyAsHtml $body -Encoding ([System.Text.Encoding]::UTF8)
如果 SMTP 服务器为 SMTP over TLS 使用特定端口,请使用 -Port
参数:
If the SMTP server uses a specific port for SMTP over TLS, use the -Port
parameter:
Send-MailMessage -SmtpServer $smtpserver -Port 465 -UseSsl -From $from -To $recipient -Subject $subject -BodyAsHtml $body -Encoding ([System.Text.Encoding]::UTF8)
如果您想确保始终协商 TLS(而不是 SSL 3.0),请在 ServicePointManager
类上设置 SecurityProtocol
属性:
If you want to make sure that TLS is always negotiated (and not SSL 3.0), set the SecurityProtocol
property on the ServicePointManager
class:
[System.Net.ServicePointManager]::SecurityProtocol = 'Tls,TLS11,TLS12'
这篇关于我可以将 TLS 与 Send-MailMessage cmdlet 一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文