Author Posts

December 26, 2017 at 2:19 pm

Hello!

I get this error when trying to send a mail via powershell.

Here is my script:

$To= "toto@gmail.com"
$From = "tata@gmail.com"
$Subject  = "Salutation"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "465"
$body = "test message"
$mdp = "toto"
$PWord = ConvertTo-SecureString –String $mdp –AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($From, $PWord)
$SMTPClient.EnableSsl = $true
Send-MailMessage -To $To -From $From -Subject $Subject -SmtpServer $SMTPServer -Body $body -Credential $Credential  -Port $SMTPPort 

Here is the error i got:

Send-MailMessage : Impossible de lire les données de la connexion de transport : net_io_connectionclosed.
Au caractère Ligne:11 : 1
+ Send-MailMessage -To $To -From $From -Subject $Subject -SmtpServer $S ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation : (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

Thank's for your help.

December 26, 2017 at 10:03 pm

Are you able to test connectivity to smtp.gmail.com and is that successful.

Test-NetConnection -Port 465 -ComputerName smtp.gmail.com -InformationLevel Detailed

December 26, 2017 at 11:28 pm

HI.
As mentioned above first try Test-NetConnection. If this passes. Then ensure assuming your gmail account is a free account that the gmail account you are trying to send has not reached the maximum send limit, I believe its maximum 2000 messages per day. The Error message net_io_connectionclosed is pretty generic and doesn't directly show what the underlying issue is. if you want to dig deep a wireshark packet capture would give you a better idea whats happening

alternatively you can try the below with the -useSSL switch and port changed to 587


$To= "toto@gmail.com@gmail.com"
$From = "toto@gmail.com@gmail.com"
$Subject  = "Salutation"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$body = "test message"
$mdp = "toto"
$PWord = ConvertTo-SecureString –String $mdp –AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ($From, $PWord)
Send-MailMessage -To $To -From $From -Subject $Subject -SmtpServer $SMTPServer -Body $body -Credential $Credential  -Port $SMTPPort -UseSsl

hope this helps
regards

December 27, 2017 at 1:10 am

There are some issues in your code.
See this article for details:
'pdq.com/blog/powershell-send-mailmessage-gmail'

December 27, 2017 at 2:40 pm

Hello!

Here is the informations I get

ComputerName             : smtp.gmail.com
RemoteAddress            : 74.125.206.109
RemotePort               : 465
AllNameResolutionResults : 2a00:1450:400c:c04::6c
                           74.125.206.108
                           74.125.206.109
MatchingIPsecRules       : 
NetworkIsolationContext  : Internet
InterfaceAlias           : Wi-Fi
SourceAddress            : MyIP
NetRoute (NextHop)       : MyIP
PingSucceeded            : True
PingReplyDetails (RTT)   : 18 ms
TcpTestSucceeded         : True

Thank's