Send Mail

This topic contains 4 replies, has 4 voices, and was last updated by  Arnold 3 weeks, 4 days ago.

  • Author
    Posts
  • #90028

    Arnold
    Participant

    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.

  • #90046

    Alex Aymonier
    Participant

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

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

      Arnold
      Participant

      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

  • #90053

    Shihan Pietersz
    Participant

    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

  • #90058

    postanote
    Participant

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

You must be logged in to reply to this topic.