Powershell 如何使用Gmail作为SMTP服务器发送邮件

Powershell 如何使用Gmail作为SMTP服务器发送邮件,powershell,Powershell,我正在使用此代码通过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.S

我正在使用此代码通过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) 使用“1”参数调用“发送”时出现异常:“SMTP服务器需要 安全连接或客户端未通过身份验证。 服务器响应为:需要5.5.1身份验证。 +$smtp.Send($message)
我做错了什么?

试试这个:似乎是同一个问题。我看到的可能是重复的,但它得到了相同的错误。请阅读答案下面的评论,通过“你必须执行一些gmail配置”这个答案(直接链接)-试试这个:似乎是同一个问题。可能是重复的我已经看到了这一点,但它得到了相同的错误。请阅读下面的评论回答“有一些gmail配置,你必须执行”这个答案(直接链接)-