我正在使用以下代码通过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)
我在做什么错了?