Я использую этот код для отправки почты с помощью PowerShell:
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "user@gmail.com"
$Password = "pass"
$to = "mail"
$subject = "TRans LOG"
$body = "TRANS log info"
$message = New-Object System.Net.Mail.MailMessage
$message.Subject = $subject
$message.Body = $body
$message.To.Add($to)
#$message.Cc.Add($cc)
$message.From = $username
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.Send($message)
Write-Host "Mail Sent successfully using GMAIL"
Но я получаю сообщение об ошибке:
Exception calling "Send" with "1" argument(s): "The SMTP server requires a
secure connection or the client was not authenticated.
The server response was: 5.5.1 Authentication Required.
+ $smtp.Send($message)
Что я делаю не так?