Search code examples
azuresmtpsendgridazure-powershell

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 ="xxxxx@azure.com"
$Password = ConvertTo-SecureString "xxxx" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $Username, $Password
$SMTPServer = "smtp.sendgrid.net"
$EmailFrom = "from@mail.com"
$EmailTo = "to@mail.com"
$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.


Solution

  • 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.