PowerShell 5.1 marks the error described below the code I can't find a solution

<p class="">#setting stmp</p> $SMTPServer = "*******.*****.***"$SMTPServerPort = "587"

$Username = “@.**”

$Password = “******”

$smtp.EnableSsl = $true

#corpo mail

$to = “*******@**.

$subject = “Results for AD Report LAST LOGON”

$body = “Script version 1.0 by Maarten Mol”

$attachment = “C:\prova.txt”

$message = New-Object System.Net.Mail.MailMessage

$message.subject = $subject

$message.body = $body


$message.from = $username


$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);

$smtp.EnableSSL = $true

$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);




Exception during the “Send” argument with argument “4”: "The SMTP server requires a secure connection or the client was not authenticated. Server response: 5.7.1 The client was not authenticated"In C:\Users\Roberto.Bugatti\Desktop\invioemail\mailsender.ps1:27 car:1 + $smtp.send($to,$Username,$body,$attachment)

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SmtpException

It would appear that you’re not successfully authenticating with your smtp server, most likely requires SSL or something similar.

Also, why use the .net providers here, when Powershell has a native cmdlet send-mailmessage.

I see port 587, I think Office 365, but regardless here is an example of using Send-MailMessage:

Not the best example but it works



$login = "Login name" $password = "Password" | Convertto-SecureString -AsPlainText -Force $credentials = New-Object System.Management.Automation.Pscredential -Argumentlist $login,$password
Send-MailMessage -SmtpServer smtp.office365.com -from "From@email.nl" -credential $credentials -to "To@email.nl" -subject "Subject" -Body "Body" -UseSsl