1

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.

Pradeep
  • 5,101
  • 14
  • 68
  • 140

2 Answers2

2

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

user1713059
  • 1,425
  • 2
  • 17
  • 34
1

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.

Nancy
  • 26,865
  • 3
  • 18
  • 34
  • I'm trying to run this command on `mac` os `Test-NetConnection` to know the information of `smtp.sendgrid.net` connection. But I'm getting the error `Not able find the command Test-NetConnection` – Pradeep Dec 28 '20 at 16:18
  • How to test connectivity of smtp server on `mac` os instead of using Telnet? – Pradeep Dec 28 '20 at 16:19
  • Try `nc -vz (your router's IP address) (port)` in the terminal. More details [here](https://www.wikihow.com/Check-if-a-Port-Is-Opened) – Nancy Dec 29 '20 at 01:52
  • Is there any alternative approach for sending email with recent deployment details of Azure resources? – Pradeep Dec 29 '20 at 04:37
  • 1
    Have you tried the method in my shared [link](https://4bes.nl/2020/01/19/send-email-from-powershell-with-sendgrid/)? – Nancy Dec 29 '20 at 05:20
  • Thanks @Nancy, it is working with send grid api key instead of username and password – Pradeep Dec 30 '20 at 09:21
  • Nancy, I am able to send email with send grid api key by following this [link](https://4bes.nl/2020/01/19/send-email-from-powershell-with-sendgrid/). But I want to send email to multiple receivers. So, can you please suggest me how to add multiple email addresses in the `to` section. – Pradeep Jan 12 '21 at 16:23
  • Try to use "to" = @( @{ "email" = "exa1@mp.le" "name" = "Receiving party" }, @{ "email" = "exa2@mp.le" "name" = "Receiving party" } )? – Nancy Jan 13 '21 at 07:59
  • Yes it is working. But I'm looking the array format for passing list of email addresses. – Pradeep Jan 13 '21 at 15:27