Yrox Mk
Yrox Mk

Reputation: 49

The remote certificate is invalid when trying to send mail

I'm trying to send user a password reset link but when it comes to the Smtp.send stage, I got error saying that The remote certificate is invalid according to the validation procedure.

I'm not using Gmail to send this, I'm using our own domain mail server. Will it be issue for this matter ?

this is the sample of the code.

var smtp = new SmtpClient {
  Host = "mail.sample.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromEmail.Address, fromEmailPassword)

};
using(var message = new MailMessage(fromEmail, toEmail) {
  Subject = subject,
    Body = body,
    IsBodyHtml = true
})

smtp.Send(message);

Upvotes: 0

Views: 2121

Answers (1)

Yrox Mk
Yrox Mk

Reputation: 49

I resolved it by adding these codes to mine

This line added before the using(var message = new MailMessage(fromEmail, toEmail) {

 ServicePointManager.ServerCertificateValidationCallback =new RemoteCertificateValidationCallback(ValidateServerCertificate);

and this method

public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
  if (sslPolicyErrors == SslPolicyErrors.None)
    return true;
  else {
    return true;

  }
}

This resolved my issue. Main link "The remote certificate is invalid according to the validation procedure." using Gmail SMTP server

Upvotes: 1

Related Questions