Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Why?

like image 333
Josh Avatar asked Apr 03 '16 12:04

Josh


1 Answers

To resolve the issue, just before the send command:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true }
like image 129
Josh Avatar answered Oct 12 '22 14:10

Josh