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;
}
}