Josh
Josh

Reputation: 365

SMTPClient Failure During Send Concerning Arguments

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?

Upvotes: 9

Views: 22651

Answers (3)

Josh
Josh

Reputation: 365

To resolve the issue, just before the send command:

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

Upvotes: 14

Aimee_King
Aimee_King

Reputation: 29

$Smtpclient.EnableSsl = ($false)

it solved my problem :)

Upvotes: 2

arreagac
arreagac

Reputation: 51

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.

Upvotes: 5

Related Questions