关于Windows:使用“ 1”作为参数调用“ Send”的异常:“根据验证过程,远程证书无效。

Exception calling “Send” with “1” argument(s): "The remote certificate is invalid according to the validation procedure. PowerShell

我正在尝试设置电子邮件PowerShell脚本,当任务计划程序运行脚本时,它将允许我发送电子邮件。 我得到的问题是:

1
Exception calling"Send" with"1" argument(s):"The remote certificate is invalid according to the validation procedure."

我正在运行以下代码:

1
2
3
4
5
6
7
8
9
10
11
$SMTPClient = New-Object Net.Mail.SmtpClient("SMTP", PORT)
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username","password")
$MailMessage = (New-Object system.net.mail.mailmessage)
$MailMessage.from = ("FROM")
$MailMessage.To.Add("TO")
$MailMessage.Subject = ("Subject")
$MailMessage.Body = ("Body")
$Smtpclient.EnableSsl = ($true)
$SmtpClient.Timeout = (300000)
$SmtpClient.Send($MailMessage)
Write-Output"Message sent";

它给了我上面指定的错误。 我在Windows Server 2012 R2上运行此代码,并且此代码在正常的Windows 10、8和7上运行,但在服务器上不行。 请有人能给我一个快速简便的解决方案。 在开发的这个阶段,安全性并不是真正的问题,因此欢迎任何解决方案。 谢谢


经过大量研究,研究和确认,结果表明。 答案将在send命令之前添加:

1
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true }

感谢您的所有帮助,Avshalom和Alexander Obersht


这仍然无法解决以下事实:证书有问题,可能与主机,过期的证书或中间证书不匹配。 通过在调用Send之前将上面的类添加到脚本中,您将禁用证书验证,这仅在不需要解决证书问题时才是一种解决方法。


1
$Smtpclient.EnableSsl = ($false)

它解决了我的问题:)