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