Asked by:
Send Email ASP.Net (MVC)

Question
-
User-322454893 posted
I've a form in my website and I want to collect data and send it by email.
I've created a new model to create the form and I've created [http] Controller to send email, but I have the following error:
"The remote certificate is invalid according to the validation procedure."
What's this error and how can I solve it?
Thursday, March 14, 2019 2:48 PM
All replies
-
User753101303 posted
Hi,
It happens when sending the mail using SSL ? You could use https://docs.microsoft.com/en-us/dotnet/api/system.net.servicepointmanager.servercertificatevalidationcallback?view=netframework-4.7.2 to define your own procedure and see first which exact https://docs.microsoft.com/en-us/dotnet/api/system.net.security.sslpolicyerrors?view=netframework-4.7.2 you have.
One could perhaps suggest to return true to just skip this validation but it would be much better to fix the source issue or to ignore a particular error for a particular certificate if you really have to. Else you would just blindy approve ALL certitficates which is certainly not what you want.
Edit: for example https://social.msdn.microsoft.com/Forums/en-US/ee556971-d914-4845-a525-5e9a4962bccb/smtpclient-and-ssl?forum=netfxnetcom
You don't have a stacktrace? According to this post the stacktrace could contain already the root cause. If having your own exception handler you should really log more than ex.Message for developers.
Thursday, March 14, 2019 3:32 PM -
User-322454893 posted
This is the code:
[HttpPost]
public ActionResult TryMe(EmailFormModel model)
{
try
{
bool validData = IsValidData(model);if (model != null && validData)
{
var senderEmail = new MailAddress("***");
var receiverEmail = new MailAddress("***");
var password = "*******";
var subject = "Online Service";
var body = GetMailBody(model);
var smtp = new SmtpClient
{
Host = "***.**.**.***",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(senderEmail.Address, password)
};ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) =>
{
if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
return true;// if there are errors in the certificate chain, look at each error to determine the cause.
if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
{
if (chain != null && chain.ChainStatus != null)
{
foreach (var status in chain.ChainStatus)
{
if ((certificate.Subject == certificate.Issuer) && (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
{
// self-signed certificates with an untrusted root are valid.
continue;
}
else if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
{
// if there are any other errors in the certificate chain, the certificate is invalid,
// so the method returns false.
return false;
}
}
}// When processing reaches this line, the only errors in the certificate chain are
// untrusted root errors for self-signed certificates. These certificates are valid
// for default Exchange server installations, so return true.
return true;
}return false;
};using (var msg = new MailMessage(senderEmail, receiverEmail)
{
Subject = subject,
Body = body
})
{
smtp.Send(msg);
ViewBag.Verified = "Data Sent!";
}return View();
}
else
{
ViewBag.Warning = "Fill missed data!";
}
}
catch (Exception ex)
{
ViewBag.Warning = ex.Message;
}return View();
}When I try to send an Email, I get this Error:
"The remote certificate is invalid according to the validation procedure."
Monday, March 18, 2019 11:21 AM -
User753101303 posted
Ok I see now that you are showing ex.Message which is not that useful to developers (and users). Have a look at ex.Tostring() which could already show the root cause as part of the strack trace and the inner exception(s) chain.
Also adding the VS validation callback is not to solve the issue but so that you can use https://docs.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2017 to see how the code runs and which exact problem is reported about this certificate.
Monday, March 18, 2019 12:05 PM -
User-322454893 posted
Ok, now sslPolicyErrors has this value: "RemoteCertificateNameMismatch"
What does that mean and to solve it?
Monday, March 18, 2019 12:15 PM -
User753101303 posted
Hi,
And the Host is correct ? If not done yet try the server full name ie "smtp.mydomain.com" rather than an IP address or "smtp". The name must exactly match what is allowed by the certificate.
If it still fails you may have to inspect the certificate (either programmatically or see perhaps with those in charge of this server so that they give the exact name you should use).
Edit: finally you could ignore this error if there is something really wrong with the current configuration that can't be fixed before you need that to work but then :
- make sure to ignore just the EXACT error that happens rather than accepting any error that could happen
- it should be still a temporary fix and removed as soon as the root cause has been finally fixedMonday, March 18, 2019 1:33 PM -
User-322454893 posted
OK, I have used the full host name instead of the IP address, and now sslPolicyErrors is None and it is sending email in the debug mode before I publish it, but after publishing it and trying to send an email, it gives me this error:
"The remote certificate is invalid according to the validation procedure."
Why?
Update:
I've set EnableSsl = false, but the same error message
"The remote certificate is invalid according to the validation procedure."
is still exist... How is that?
Monday, March 18, 2019 2:39 PM -
User753101303 posted
Try to remove Port=587 if not done already.
Seems you are back at trying to understand the problem based on ex.Message. This is not enough. A developer should start from ex.ToString() to get full details about the exception that happens.
Monday, March 18, 2019 3:47 PM -
User-893317190 posted
Hi Elgamily,
Some smtp servers may have their own restriction for 3rd party mail clients.
Please ensure your smtp server is available.
I have used gmail to have a test and successfully sent my email.
Below is my code.
using (SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587)) { smtp.EnableSsl = true; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //to authenticate we set the username and password properites on the SmtpClient smtp.Credentials = new NetworkCredential("my username", "my password"); using (MailMessage mail = new MailMessage()) { mail.From = new MailAddress("my address"); mail.To.Add("my address"); //set the content mail.Subject = "my subject"; mail.Body = "my body"; smtp.Send(mail); } //set the addresses }
Maybe you could change a smtp server to check where the problem is with your smpt server.
Best regards,
Ackerly Xu
Tuesday, March 19, 2019 4:03 AM -
User-322454893 posted
I have used ex,ToString(), but I've the same error: "The remote certificate is invalid according to the validation procedure."
and I have tried port 25, but it gives me the same error too.
Update:
I have used this code too:
[HttpPost]
public ActionResult TryMe(EmailFormModel model)
{
try
{
bool validData = IsValidData(model);if (model != null && validData)
{
var host = "***";
var senderEmail = new MailAddress("***");
var receiverEmail = new MailAddress("***");
var password = "***";
var subject = "Online Service";
var body = GetMailBody(model);using (SmtpClient smtp = new SmtpClient(host, 587))
{
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//to authenticate we set the username and password properites on the SmtpClient
smtp.Credentials = new NetworkCredential(senderEmail.Address, password);using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(senderEmail.Address);
mail.To.Add(receiverEmail);
//set the content
mail.Subject = subject;
mail.Body = body;smtp.Send(mail);
ViewBag.Verified = "Data Sent!";
}
}return View();
}
else
{
ViewBag.Warning = "Fill missed data!";
}
}
catch (Exception ex)
{
//ViewBag.Warning = ex.Message;
ViewBag.Warning = ex.ToString();
}return View();
}but I have the same error: "The remote certificate is invalid according to the validation procedure."
Tuesday, March 19, 2019 7:47 AM -
User-893317190 posted
Hi Elgamily,
What smtp server do you use ?
Could you establish ssl connection?
I suggest you could change to another server to test whether your server is valid.
Best regards,
Ackerly Xu
Tuesday, March 19, 2019 9:03 AM -
User-322454893 posted
I'm using hostgator smtp server
How to test ssl connection?
Tuesday, March 19, 2019 9:06 AM -
User-893317190 posted
Hi Elgamily,
Not sure about hostgator smtp server, but it seems you should configure your account and then send email , please refer to the link below.
https://www.youtube.com/watch?v=UvhBPf5NTEY
If it still doesn't work , please go to their forum to check whether you have right configuration.
Best regards,
Ackerly Xu
Tuesday, March 19, 2019 9:47 AM