我有以下脚本,用于在运行 PowerShell 程序时发送电子邮件,它通常工作得非常好:
$msg = New-Object System.Net.Mail.MailMessage
$msg.From = $from
$msg.To.Add($to)
$msg.Subject = $subject
$msg.Body = $html
$msg.IsBodyHtml = $true
$smtp = New-Object System.Net.Mail.SmtpClient($server)
$smtp.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
try
{
$smtp.Send($msg)
Write-Host "Email sent to $to without error"
}
catch
{
$retryCount++
Write-Warning "Email failed to send. $_.Exception.Message"
}
问题是我已经开始在我刚刚设置的 VMWorkstation 机器上使用这个脚本,此时我无法发送电子邮件。这是我得到的错误:
WARNING: Email failed to send. 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.7.1 Client was not authent
icated".Exception.Message
我相信我收到此错误是因为我使用:
$smtp.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
是这样吗?如果是这样,有没有办法可以从我的虚拟机中提取我的凭据?我正在使用桥接网络,所以我不知道为什么我无法通过该呼叫访问它们。我还更改了我公司网络的所有域权限。
任何见解都会有所帮助。