2016-10-10 53 views
0

我想使用PowerShell发送电子邮件,但需要使用TLS。我可以使用Send-MailMessage cmdlet来做到这一点吗?我可以将TLS与Send-MailMessage cmdlet一起使用吗?

这是我的代码:

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

if (test-path $file) 
{ 

    $from = "[email protected]" 
    $to = "<[email protected]>","<[email protected]>" 
    $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" 
} 

非常感谢!

回答

0

确保您指定-UseSsl开关:

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

如果SMTP服务器用于SMTP 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),请在ServicePointManager类别上设置SecurityProtocol属性:

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

但它说SSL。它是否使用TLS? –

+1

它协商任何服务器提供的,默认情况下最高的TLS 1.0或SSL 3.0 –

+0

我明白了。那么,如果Office 365使用TLS 1.2,对吧? –

相关问题