Asp.Net Core - The remote certificate is invalid according to the validation procedure.
Dung Do Tien Mar 29 2021 412
Hello,
I have a project with Asp.Net Core 3.1 and It's has a function to send mail for Forget Password page. It's as below:
public static class EmailSender
{
private static async Task SendAsync(string credentialEmail, string credentialPassword, int port, string host,
List<string> to, string subject, string body, string senderEmail, string senderName)
{
//Initial mail server
var smtpServer = new SmtpClient
{
Port = port, //587
Host = host, //"smtp.gmail.com"
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(credentialEmail, credentialPassword)
};
var mail = new MailMessage();
mail.From = new MailAddress(senderEmail, senderName, Encoding.UTF8);
for (var i = 0; i < to.Count; i++)
mail.To.Add(to[i]);
mail.Subject = subject;
mail.Body = body;
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mail.IsBodyHtml = true;
await smtpServer.SendMailAsync(mail);
}
}
But when I call this function with full information :
Port: 587 Host: 1.2.3.4.343
But I got an error: System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
I don't know what's about the certificate? Thanks for any suggestions.
Have 1 answer(s) found.
- đ0
đặng thái sơn Mar 29 2021
This error because your host is not trusted. You need to trust your hots by the domain(not use IP such 1.2.3.45).
Or If you want to use Ip you can add the code below into constructor of
SenderEmail
class:static EmailSender() { ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; }
This code helps auto trust SMTP protocol for you.
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.