Can someone spot my mistake?

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 = “Gary@test.net
$Password = Get-content C:\test\Scripts\password.txt | ConvertTo-SecureString
$cred= New-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
$from = “Gary@test.net
$to = “henryga@domain.com
$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

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).

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 :frowning: . If I read your note correctly, that should have addressed your suggestion correct?

thanks !

Change the security protocol used for the email server authentication:

# 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 = $From
To = $To
Subject = $Subject
Body = $Body
SmtpServer = $SMTPServer
Port = $SMTPPort
UseSsl = $true
Credential = $cred
}

Send-MailMessage @splat

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 :)

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 :slight_smile:

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

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

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

@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.