SOLVED

Send email by powershell script is working on server 2019,but show error on windows server 2016

Copper Contributor

$username = "Email address removed"
$password = "aaaaaabbbbb"
$sstr = ConvertTo-SecureString -string $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -argumentlist $username, $sstr
$body = "This is a test email"
Send-MailMessage -To "Email address removed" -From "Email address removed" -Subject 'Test message' -Body $body -BodyAsHtml -SmtpServer smtp.office365.com -UseSSL -Credential $cred -Port 587

 

Run this PS script that send email from server , if running on windows server 2019 ,it's fine.

but show error on windows server 2016.

anyone know it?

 

 

Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed.
At E:\test\test.ps1:6 char:1
+ Send-MailMessage -To "Email address removed" -From "Email address removed ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

 

4 Replies
My first guess would be TLS settings, could you try using [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 before the send-mailmessage?
best response confirmed by silverlu1210 (Copper Contributor)
Solution
Thanks guy, Issue resolved. It means TLS should use 1.2 right?
Yes, you can put in in your scripts running on server 2016 or change the server settings to enable it by default https://www.itnota.com/enabling-tls-1-2-default-security-protocol-windows-servers/. Please mark my answer as solution to mark it as solved
1 best response

Accepted Solutions
best response confirmed by silverlu1210 (Copper Contributor)
Solution
Thanks guy, Issue resolved. It means TLS should use 1.2 right?

View solution in original post