简体   繁体   中英

ASP.Net MVC 5 Sending Email error: The remote certificate is invalid according to the validation procedure

I have an ASP.Net MVC 5 application when I send email get bellow error:

The remote certificate is invalid according to the validation procedure.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[AuthenticationException: The remote certificate is invalid according to the validation procedure.]
System.Net.Mail.ConnectAndHandshakeAsyncResult.End(IAsyncResult result) +113 System.Net.Mail.SmtpClient.ConnectCallback(IAsyncResult result) +61

[SmtpException: Failure sending mail.]
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84 VRM5.Controllers.d__5.MoveNext() in C:\\Users\\mehdi\\Documents\\Visual Studio 2015\\Projects\\VRM5\\VRM5\\Controllers\\VRsController.cs:289
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +143
System.Web.Mvc.Async.<>c__DisplayClass37.b__36(IAsyncResult asyncResult) +23
System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d() +112 System.Web.Mvc.Async.<>c__DisplayClass46.b__3f() +452 System.Web.Mvc.Async.<>c__DisplayClass33.b__32(IAsyncResult asyncResult) +15
System.Web.Mvc.Async.<>c__DisplayClass2b.b__1c() +37 System.Web.Mvc.Async.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult) +241
System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +19
System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +51
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +195

my code for sending email:

var message = new MailMessage();
//-----Send Email------------------------------------------//
message.To.Add(new MailAddress(Q_contacts.ContactEmail));  // replace with valid value 
if(CheckAD==1)
{
    message.CC.Add(new MailAddress( mehdi@hotmail.com"));
}

message.From = new MailAddress(vR.Email);  // replace with valid value
message.Subject = "Vehicle Request -"+vR.Distination;
//message.Body = string.Format(body, model.FromName, model.FromEmail, model.Message);
message.Body = MS_1;
message.IsBodyHtml = true;

using (var smtp = new SmtpClient())
{
    var credential = new NetworkCredential
    {
        UserName = "mehdi@abc.org",  // replace with valid value
        Password = "Abc@abc123"  // replace with valid value
    };
    smtp.Credentials = credential;
    smtp.Host = "mr09.hv.abc.org";
    smtp.Port = 25;
    smtp.EnableSsl = false;
    await smtp.SendMailAsync(message);
    return RedirectToAction("Index");
}

The issue you see here is related to the SSL certificate used for the SMTP service offered by the remote email environment.

To check the SSL certificate you can use openSSL via:

openssl s_client -connect exchange01.int.contoso.com:25 -starttls smtp

Depending on the configuration in your situation multiple solutions might apply here (see here for more infos):

  • The hostname isn´t in the SSL certificate. Then the remote mail server must be reconfigured.
  • The remote mail server still used an self signed certificate. You can try to import that on your server (see link above).
  • Some SSL certificates used here are outdated. Then the remote mail server must be reconfigured.
  • Some parts from the certification chain aren´t trusted. To solve the issue you need to import them into the trusted SSL store on your server (see link above).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM