简体   繁体   中英

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

I am trying to setup an email PowerShell script that will let me send an email when the task scheduler runs the script. The problem i am getting is:

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

I am running the following code:

$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";

It gives me the error as specified above. I am running this on a Windows Server 2012 R2 and this code does work on the normal Windows 10, 8 and 7 but not Servers. Please can someone give me a quick and easy solution. Security really isn't a problem at this stage of development so any solution is welcome. Thank you

It turned out after much research and looking and then confirming. The answer is to be added just before the send command:

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

Thanks for all your help Avshalom and Alexander Obersht

This still doesn't resolve the fact the you have an issue with your certificate, perhaps a mismatch with host, expired certificate or intermediate certificate. By adding this class above to your script before the call to Send you are disabling certificate validation, this is a workaround only if you do not need to address the certificate issue.

$Smtpclient.EnableSsl = ($false)

它解决了我的问题:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM