Can someone spot my mistake?

Welcome Forums General PowerShell Q&A Can someone spot my mistake?

Viewing 6 reply threads
  • Author
    Posts
    • #283627
      Participant
      Topics: 2
      Replies: 4
      Points: 34
      Rank: Member

      All

      I am trying to figure out what I am doing wrong with this.  I am trying to send an email with Send-MailMessage.  I found some posts online and some suggested that this error is because my password is Not correct.   I am very sure that my password is correct, I have recreated the file twice.

      this is what  I am doing..

      I created the password file with this command: 

      read-host -AsSecureString | ConvertFrom-securestring | Out-File C:\test\Scripts\password.txt

      I execute this script:

      $username = “[email protected]
      $Password = Get-content C:\test\Scripts\password.txt | ConvertTo-SecureString
      $cred= New-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
      $from = “[email protected]
      $to = “[email protected]
      $smtpserver = “smtp.gmail.com”
      $smtpport = “465”
      $subject = “test message from Powershell”
      $body = “this is the body of the email entered from Powershell”

      Send-MailMessage -From $From -to $To -Subject $Subject
      -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl

      -Credential $cred

      This is the error I am getting

      Send-MailMessage : Unable to read data from the transport connection: net_io_connectionclosed.
      At line:1 char:1
      + Send-MailMessage -From $from -to $to -Subject $subject -body $body -S …
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
      ion
      + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

      Any tips, suggestions or guidance would be amazing!

      Thanks

      Gary

    • #283660
      Participant
      Topics: 11
      Replies: 132
      Points: 896
      Helping Hand
      Rank: Major Contributor

      I don’t see an immediate issue with your code, and I tried it for kicks on my end with success (365, not Gmail).

      Assuming the password and server settings are all correct (try port 587?), and MFA/app passwords are not enabled on the account, are you running the script with the same user access token as the one that generated the standard encrypted string for the text file?

      For instance, using non-elevated PowerShell session to output the standard encrypted string to the text file, but then using an elevated PowerShell session to run the script (vice versa) will result in the wrong password being supplied for the credential object. Whichever access token was used to output the encrypted string to the text file needs to be the same token that runs the script (both non-elevated or both elevated).

    • #283840
      Participant
      Topics: 2
      Replies: 4
      Points: 34
      Rank: Member

      Aaron

      Thanks for the answer..

      If I use pot 587 and If I read the documentation correctly, I  need to specify STARTTLS/  To be honest, I do not know how to do that and could not find any examples.  The way I understand it, in the google world,  port 465 is SSL hence why I used, -USEssl switch

      As for the password, I thought there was a problem like you described so I opened an Admin PS windows,  created a new password file  and then executed each command manually in the same window.   at the end of the Send-MailMessage command I got the same error 🙁  .  If I read your note correctly, that should have addressed your suggestion correct?

      thanks !

    • #283858
      Participant
      Topics: 11
      Replies: 132
      Points: 896
      Helping Hand
      Rank: Major Contributor

      Change the security protocol used for the email server authentication:

      I’m using splatting to keep things a little cleaner. When posting code, use the Preformatted  option (where you see Paragraph) to make your code easier to read 🙂
    • #283972
      Participant
      Topics: 2
      Replies: 4
      Points: 34
      Rank: Member

      Thanks for your response but it turns out that somehow it is”google” causing the problem.

      When Aarons’ script still did not work with gmail,  I decided to try one of my personal hotmail accounts.  With his  tip for STARTLS,  all  I had to do is to create a new password file,   change the From email and point to the Hotmail servers  and   it worked first time 🙂

      I know this is not the place for this question but does anyone might know why?  I need to send emails from that gmail account so any ideas are welcome!

      thanks again!

      Gary

    • #284026
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,987
      Helping Hand
      Rank: Community Hero

      If you have MFA enabled, then you have to create an app password:

      https://support.google.com/mail/answer/185833?hl=en

    • #284113
      Participant
      Topics: 10
      Replies: 201
      Points: 987
      Helping Hand
      Rank: Major Contributor

      @henryga79 FYI, I used your exact script and simply changed the port to 587 and it worked. I got the same error as you initially.

Viewing 6 reply threads
  • You must be logged in to reply to this topic.