Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
System.Net.Mail.SmtpException
  HResult=0x80131500
  Message=Failure sending mail.
  Source=studentlog
  StackTrace:
   at studentlog.Controllers.StudentController.Sendmail(MailMessage mail) in C:\Users\syashwan\source\repos\studentlog\Controllers\StudentController.cs:line 157
   at studentlog.Controllers.StudentController.BuildEmailtemplate(String SubjectText, String BodyText, String sendTo) in C:\Users\syashwan\source\repos\studentlog\Controllers\StudentController.cs:line 139
   at studentlog.Controllers.StudentController.BuildEmailtemplate(Int32 regId) in C:\Users\syashwan\source\repos\studentlog\Controllers\StudentController.cs:line 110
   at studentlog.Controllers.StudentController.Create(tblstapp tblstapp, AccountViewModel accountViewModel, Users usdata) in C:\Users\syashwan\source\repos\studentlog\Controllers\StudentController.cs:line 68
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c.<BeginInvokeSynchronousActionMethod>b__9_0(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2()

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

Inner Exception 2:
SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond


What I have tried:

public ActionResult confirm(int regId)
       {
           ViewBag.regId = regId;
           return View();
       }
       public JsonResult RegisterConfirm(int regId)
       {
           tblstapp data = db.tblstapps.Where(x => x.Id == regId).FirstOrDefault();
           data.IsValid = true;
           db.SaveChanges();
           var msg = "Your email is verified";
           return Json(msg, JsonRequestBehavior.AllowGet);
       }
       public void BuildEmailtemplate(int regId)
       {
           string body = System.IO.File.ReadAllText(HostingEnvironment.MapPath("/EmailTemplate/") + "Text" + ".cshtml");
           var regInfo = db.tblstapps.Where(X => X.Id == regId).FirstOrDefault();
           var url = "https://localhost:44353/" + "Student/Confirm?regId=" + regId;
           body = body.Replace("@viewBag.ConfirmationLink", url);
           body = body.ToString();
           BuildEmailtemplate("Your account Created succefully", body, regInfo.Email);

       }

       public static void BuildEmailtemplate(string SubjectText,string BodyText,string sendTo)
       {
           string from, to, bcc, cc, subject, body;
           from = "Your email";
           to = sendTo.Trim();
           bcc = "";
           cc = "";
           subject = SubjectText;
           StringBuilder sb = new StringBuilder();
           sb.Append(BodyText);
           body = sb.ToString();
           MailMessage mail = new MailMessage();
           mail.From = new MailAddress(from);
           mail.To.Add(new MailAddress(to));
           if (!string.IsNullOrEmpty(bcc))
           {
               mail.Bcc.Add(new MailAddress(bcc));
                   }
           if (!string.IsNullOrEmpty(cc))
           {
               mail.Bcc.Add(new MailAddress(cc));
           }
           mail.Subject = subject;
           mail.Body = body;
           mail.IsBodyHtml = true;
           Sendmail(mail);

       }
       public static void Sendmail(MailMessage mail)
       {
           SmtpClient client = new SmtpClient();
           client.Host = "smtp.gamil.com";
           client.Port = 587;
           client.EnableSsl = true;
           client.UseDefaultCredentials = false;
           client.DeliveryMethod = SmtpDeliveryMethod.Network;
           client.Credentials = new System.Net.NetworkCredential("Your email", "Password");
           try
           {
               client.Send(mail);
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }
Posted
Updated 11-Sep-22 4:47am

1 solution

Are you sure that SMTP host name is correct, "smtp.gamil.com"?
 
Share this answer
 
Comments
Randomuser787 13-Sep-22 14:03pm    
-----I have followed this tutorial for sending mail after registering int the mvc5-
https://www.youtube.com/watch?v=yQtDFZkbiHg&t=38s------------------------------------


-------"I tried the same project in other pc, and this came up."-----

System.Security.Authentication.AuthenticationException
HResult=0x80131501
Message=The remote certificate is invalid according to the validation procedure.
Source=mscorlib
StackTrace:
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at studentlog.Controllers.StudentController.Sendmail(MailMessage mail) in C:\Users\harsh\Desktop\studentlog\Controllers\StudentController.cs:line 160

This exception was originally thrown at this call stack:
[External Code]
studentlog.Controllers.StudentController.Sendmail(System.Net.Mail.MailMessage) in StudentController.cs
Dave Kreskowiak 13-Sep-22 14:51pm    
Since you didn't answer my question or mention it at all, I'll just say you misspelled "smtp.gmail.com".
Randomuser787 14-Sep-22 9:50am    
Thanks i have changed that. will try now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900