Reputation: 5530
I used the below PowerShell script for sending email using Azure SendGrid account details. While executing the script, I am getting the error Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed
PoweShell Script:
$Username ="[email protected]"
$Password = ConvertTo-SecureString "xxxx" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $Username, $Password
$SMTPServer = "smtp.sendgrid.net"
$EmailFrom = "[email protected]"
$EmailTo = "[email protected]"
$Subject = "SendGrid test"
$Body = "SendGrid testing successful"
Send-MailMessage -smtpServer $SMTPServer -Credential $credential -Usessl -Port 587 -from $EmailFrom -to $EmailTo -subject $Subject -Body $Body
So, can anyone suggest me how to resolve this issue.
Upvotes: 1
Views: 34850
Reputation: 1529
I fixed this error (in non-Azure environment) by changing the security protocol to TLS 1.2:
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
Details: Powershell Setting Security Protocol to Tls 1.2
Related: SmtpException: Unable to read data from the transport connection: net_io_connectionclosed
Upvotes: 2
Reputation: 28264
It looks like a networking connection issue, you may check the followings:
For more information, it's recommended to use API Keys provided by SendGrid to send emails instead of sending passwords over the script.
Upvotes: 1