Powershell 發送郵件訊息錯誤

Powershell 發送郵件訊息錯誤

無法讓 Send-MailMessage 使用 Office365 電子郵件,因此現在嘗試使用 Gmail,但仍然無法正常運作。

發生錯誤:Send-MailMessage:SMTP 伺服器需要安全連線或用戶端未經身份驗證。伺服器回應是:5.5.1 需要身份驗證。

瀏覽了很多類似的發送郵件錯誤並嘗試了一些建議

  • 在 Gmail 安全設定中開啟“使用較不安全的應用程式”
  • 嘗試刪除 useSSL
  • 仔細檢查 Gmail 地址和密碼

身為 powershell 的新手,有人能給我指出正確的方向嗎?程式碼如下:

 $From = "[email protected]"
 $To = "[email protected]"
 $Subject = "Here's the Email Subject"
 $Body = "This is what I want to say"
 $SMTPServer = "smtp.gmail.com"
 $SMTPPort = "587"
 Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential (Get-Credential)

答案1

我認為你需要定義一個物件並將返回值分配給Get-Credential

像這樣:

$cred = Get-Credential
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $cred

來源/更多資訊:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/get-credential?view=powershell-6

相關內容