以下是用于发送邮件的代码段。直到最近才工作并且正在发送“发送邮件失败。”错误。(版本:5.1.16299.98)
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = " "
$Password = " "
$to = " "
$cc = " "
$subject = "AUTOMATED "
$message = New-Object System.Net.Mail.MailMessage
$message.Subject = $subject
$message.To.Add($to)
$message.Cc.Add($cc)
$message.From = $username
$message.IsBodyHtml = $true
$message.Body =
$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"
错误是:
Exception calling "Send" with "1" argument(s): "Failure sending mail." At F:\powershell_scripts\xxx.ps1 :88 char:1 + $smtp.Send($message) + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SmtpException
答案 0 :(得分:2)
我无法告诉你问题是什么,但我可以帮助您找到有关错误的更多信息!
try {
<your code>
}
catch {
Write-Warning $Error[0].Exception.StackTrace
throw
}
$Error
是PowerShell中的一个特殊变量,它包含已发生错误的列表。查看文档以获取更多[/正确]说明。
$Error[0]
是堆栈中的“最新”错误。
$Error
对象有很多其他属性,您可能希望查询更详细的信息......从
$Error | Get-Member
然后从那里开始。
希望这有助于您深入了解[和未来]问题[s]!