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 = “firstname.lastname@example.org”
$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
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
- FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
Any tips, suggestions or guidance would be amazing!