Welcome › Forums › General PowerShell Q&A › Can someone spot my mistake?
- This topic has 6 replies, 4 voices, and was last updated 2 weeks, 1 day ago by
Participant.
-
AuthorPosts
-
-
January 1, 2021 at 7:11 pm #283627
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 $credThis 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.SendMailMessageAny tips, suggestions or guidance would be amazing!
Thanks
Gary
-
January 2, 2021 at 3:19 am #283660
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).
-
January 3, 2021 at 3:35 pm #283840
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 !
-
January 3, 2021 at 4:40 pm #283858
Change the security protocol used for the email server authentication:
PowerShell1234567891011121314151617181920212223# you can check what the current protocol is (for kicks)[System.Net.ServicePointManager]::SecurityProtocol# in your script$protocol = [System.Net.SecurityProtocolType]::Tls12[System.Net.ServicePointManager]::SecurityProtocol = $protocol$username = “Gary@test.net”...$body = “this is the body of the email entered from Powershell”$splat = @{From = $FromTo = $ToSubject = $SubjectBody = $BodySmtpServer = $SMTPServerPort = $SMTPPortUseSsl = $trueCredential = $cred}Send-MailMessage @splatI’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 🙂 -
January 4, 2021 at 8:43 am #283972
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
-
January 4, 2021 at 11:04 am #284026
If you have MFA enabled, then you have to create an app password:
-
January 4, 2021 at 3:30 pm #284113
-
-
AuthorPosts
- You must be logged in to reply to this topic.