Pradeep
Pradeep

Reputation: 5530

Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed

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

Answers (2)

user1713059
user1713059

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

Nancy Xiong
Nancy Xiong

Reputation: 28264

It looks like a networking connection issue, you may check the followings:

  • If you can reach the SMTP port (587) on the server. You may use Telnet to test SMTP communication. Refer to this.
  • If you have typed the correct parameters like credentials.

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

Related Questions